设置传输参数的路由的正确方法是什么?
我正在尝试将当前参数 @calc
发送到 generate_csv
操作。
如何传递参数,以便我可以在 generate_csv
操作中访问它们?
我的视图
- form_tag generate_csv_path(@calc) do
= submit_tag 'Print CSV'
我的路线:
match '/generate_csv', :to => "main#generate_csv", :via => :post
resources :main
创建:
generate_csv POST /generate_csv(.:format) {:controller=>"main", :action=>"generate_csv"}
我的控制器
def generate_csv
..
替代方案:
我的控制器
def generate_csv(foobar)
我的观点
- form_tag generate_csv_path(@calc) do
= submit_tag 'Print CSV'
错误
wrong number of arguments (0 for 1)
如果我明显向它传递了一个参数,那怎么可能呢?看来它不接受它。
I am trying to send the current params @calc
to a generate_csv
action.
How can I pass the params, so I can access them within the generate_csv
action ?
my view
- form_tag generate_csv_path(@calc) do
= submit_tag 'Print CSV'
my routes:
match '/generate_csv', :to => "main#generate_csv", :via => :post
resources :main
Which creates:
generate_csv POST /generate_csv(.:format) {:controller=>"main", :action=>"generate_csv"}
my controller
def generate_csv
..
An Alternative:
my controller
def generate_csv(foobar)
my view
- form_tag generate_csv_path(@calc) do
= submit_tag 'Print CSV'
the error
wrong number of arguments (0 for 1)
How can that be if I'm obviously passing it a param? It seems its not accepting it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
link_to
:但是如果您的参数是强制性的,请更改您的路线:
否则:
Use
link_to
:But change you route, if your param is mandatory:
Else:
我认为 params[:calc] 变量应该在控制器中为您提供帮助。另外,这个问题可能会对您有所帮助。在您的视图中只需使用普通形式标签。
I think
params[:calc]
variable should help you within controller. Also, this question may help you. Within your view just use normal form tags.generate_csv_path(:calc => @calc)
应该生成链接/generate_csv?calc=VALUE
在控制器中,您可以通过 params hash 访问该值
params[:calc ]
。ps: 最近我遇到了 params hash (ruby 1.9.2、rails 3.0.3)的问题,它没有从 URL 接收查询值。它只有控制器和操作值。从 mongrel 1.2.rc 切换到 webrick 就成功了。
generate_csv_path(:calc => @calc)
should generate link/generate_csv?calc=VALUE
In controller you can access this value via params hash
params[:calc]
.p.s: Recently I had a problem with params hash (ruby 1.9.2, rails 3.0.3) which didn't receive query value from URL. It had only controller and action values. Switching from mongrel 1.2.rc to webrick did the trick.