如何在RoR中调用方法并发送新参数? [英] How to call a method and send new parameters in RoR?
问题描述
假设我有方法A
def methodA
note = Note.find(params[:id])
note.link = params[:link]
note.linktype = params[:linktype]
note.save
redirect_to(notes_url)
end
当我从这样的视图调用此方法时,它工作正常
When I call this method from a view like this, it works fine
<%= link_to image_tag(w.link, :border =>0), methodA_path(:linktype => w.linktype, :link => w.link, :id => @note.id) %>
但是,如果我从同一个控制器中的另一个方法中调用该方法,如下所示:
But, if I call the method from another method in the same controller like this:
def methodB
...
methodA(:id => params[:id], :link => link, :linktype => "image")
end
我收到此错误:
wrong number of arguments (1 for 0)
methodA获取的参数仍然与methodB获取的参数相同,而不是我从methodB发送的参数.我该如何解决这个问题?感谢阅读.
The parameters that methodA is getting are still the same parameters that methodB got, not the ones that I'm sending from methodB. How do I get around this problem? Thank for reading.
推荐答案
几件事:
- Ruby(以及Ruby on Rails)的命名约定是使用下划线表示法而不是驼峰式.因此应该是
method_a
而不是methodA
. - 看起来
methodA
是控制器操作.如果您查看方法签名,实际上并没有定义任何方法参数.这是一件好事:什么都不做. -
methodA
操作中的params
调用未访问方法参数,但正在访问Rails请求url_for
的快捷方式为您提供一些参数(其他参数在您传递的哈希中).此方法是根据您的路线自动生成的.从应用程序的根目录执行rake routes
以获得更多信息. - 如果要从
methodB
调用action方法,这可能是不明智的,则无需向其传递参数.由于methodB
也是在其自己的请求周期中被调用的操作,因此params
哈希仍可用于methodA
,并且它将找到所有这些东西.但是,我建议将任何常用功能提取到第三个帮助器方法中,并从每个操作中调用它.从其他动作中调用动作对我来说就像是代码的味道.
- The Ruby, and therefore Ruby on Rails, naming convention is to use underscore notation rather than camelcase. So it should be
method_a
rather thanmethodA
. - It looks like
methodA
is a controller action. If you look at your method signature, you're not actually defining any method parameters. That's a good thing: actions don't take any. - The
params
call in themethodA
action is not accessing method parameters, but is access the Rails requestparams
hash. - In your view, you're not actually calling the method. Rather, you're linking to the action, which, when clicked, initiate a request that is routed to that action. The actual method you're calling is
methodA_path
, which is generating the URL. This is a shortcut tourl_for
that automatically fills in some parameters for you (the other ones are in the hash you're passing). This method was automatically generated for you from your routes. Do arake routes
from the root of your app for a little more information. - If you wanted to call the action method from
methodB
, which is probably unwise, you don't need to pass it the parameters. SincemethodB
is also an action being called in its own request cycle, theparams
hash is still available tomethodA
, and it will find all of those things just fine. I'd suggest, however, extracting any common functionality into a third helper method and calling that from each action; calling actions from other actions feels like a code smell to me.
总结:methodA
和methodA_path
是不同的方法.前者不带任何参数,但访问Rails请求参数哈希,而后者则带参数以传递给url_for
.
A bit of a summary: methodA
and methodA_path
are different methods. The former takes no parameters but accesses the Rails request parameters hash, while the latter takes parameters to pass to url_for
.
This is all pretty basic, so I strongly suggest you read Agile Web Development with Rails (3rd edition for Rails 2, 4th for Rails 3).
这篇关于如何在RoR中调用方法并发送新参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!