从控制器和视图调用Ruby类方法

时间:2022-12-01 23:06:54

I've created a class method inside my Post model like so:

我在Post模型中创建了一个类方法,如下所示:

def self.target_for_link
    if @post.url == nil
        puts "_blank"
    else
        puts ""
    end
end

And I'm calling it from my controller like this:

而我正在从我的控制器中调用它,如下所示:

@target = Post.target_for_link

Yet, I'm getting a No Method error (undefined method target_for_link) with this. What am I doing wrong?

然而,我得到了一个N​​o Method错误(未定义的方法target_for_link)。我究竟做错了什么?

activerecord (5.1.3) lib/active_record/dynamic_matchers.rb:22:in `method_missing'
app/controllers/posts_controller.rb:10:in `show'
actionpack (5.1.3) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.3) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.3) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.3) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.3) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.3) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:46:in `block in serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:834:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.3) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.3) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.1.3) lib/rails/engine.rb:522:in `call'
puma (3.10.0) lib/puma/configuration.rb:225:in `call'
puma (3.10.0) lib/puma/server.rb:605:in `handle_request'
puma (3.10.0) lib/puma/server.rb:437:in `process_client'
puma (3.10.0) lib/puma/server.rb:301:in `block in run'
puma (3.10.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

1 个解决方案

#1


1  

I can't say why do you have this particular error, but your method is weird. Do you understand differences between class and instance methods?

我不能说为什么你有这个特殊的错误,但你的方法很奇怪。你了解类和实例方法之间的区别吗?

It's a class method, yet you use @port attribute as if the state mattered. Didn't you want to do something like

它是一个类方法,但你使用@port属性就好像状态很重要。你想不想做点什么吗?

def target_for_link
    if self.url == nil # you could actually ommit `self.` part here
        puts "_blank"
    else
        puts ""
    end
end

and then in controller

然后在控制器中

@post = Post.find(1)
@target = @post.target_for_link

And one more thing: why do you use puts there? I can't guess. But I think you want to return the value "_blank" or "" depending on the post url and you do it like this:

还有一件事:为什么你在那里使用put?我无法猜测。但我想你想要返回值“_blank”或“”取决于帖子网址,你这样做:

def target_for_link
    if url == nil
        "_blank"
    else
        ""
    end
end

Otherwise you would be returning the result of calling puts method which is nil.

否则你将返回调用puts方法的结果,这是nil。

#1


1  

I can't say why do you have this particular error, but your method is weird. Do you understand differences between class and instance methods?

我不能说为什么你有这个特殊的错误,但你的方法很奇怪。你了解类和实例方法之间的区别吗?

It's a class method, yet you use @port attribute as if the state mattered. Didn't you want to do something like

它是一个类方法,但你使用@port属性就好像状态很重要。你想不想做点什么吗?

def target_for_link
    if self.url == nil # you could actually ommit `self.` part here
        puts "_blank"
    else
        puts ""
    end
end

and then in controller

然后在控制器中

@post = Post.find(1)
@target = @post.target_for_link

And one more thing: why do you use puts there? I can't guess. But I think you want to return the value "_blank" or "" depending on the post url and you do it like this:

还有一件事:为什么你在那里使用put?我无法猜测。但我想你想要返回值“_blank”或“”取决于帖子网址,你这样做:

def target_for_link
    if url == nil
        "_blank"
    else
        ""
    end
end

Otherwise you would be returning the result of calling puts method which is nil.

否则你将返回调用puts方法的结果,这是nil。