如何在RoR中调用方法并发送新参数? [英] How to call a method and send new parameters in RoR?

查看:82
本文介绍了如何在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 than methodA.
  • 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 the methodA action is not accessing method parameters, but is access the Rails request params 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 to url_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 a rake 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. Since methodB is also an action being called in its own request cycle, the params hash is still available to methodA, 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.

总结:methodAmethodA_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆