Rails 中未读取传递的参数 [英] Passed parameters not being read in Rails

查看:33
本文介绍了Rails 中未读取传递的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前发布过这个问题:在 Rails 中点击按钮时传递参数

I posted this question earlier: Passing parameters on button click in Rails

这个答案非常有效.但是,我无法在 erb 中调用传递的参数,即使是从 book_now 方法传递的 @event_option_id.

And the answer works very well. However, I the passed parameters aren't able to be called in erb, even the @event_option_id that is being passed from the book_now method.

它在我的服务器日志中显示为 Parameters: {"event"=>"4", "event_option_id"=>"5"} 但是当我尝试使用 <%= @event_option_id %> 它返回空白,当我将它作为 to_i 测试时,它显示 0 而不是 5.

It is showing in my server logs as Parameters: {"event"=>"4", "event_option_id"=>"5"} but when I try to use <%= @event_option_id %> it comes back blank, and when I just tested it as to_i, it showed 0 instead of 5.

我的 EventsController 有这个方法:

def book_now
  @event_option_id = params[:event_option_id]
end 

我的链接正在传递这样的参数:

And my link is passing the parameters like this:

<%= link_to "Book This Event Now!", book_now_path(:event => @event.id, :event_option_id => e.id), :id => "book-now-button", :class => "button" %>

我试图传递这个 @event_option_id 以便我可以从我的数据库访问相应的值.

I am trying to pass this @event_option_id so that I can access the corresponding values from my db.

每个 EventOption 都有一个 pricedescriptionname,这是唯一的,当我尝试以多种方式访问​​它们,例如,我得到 (NoMethodError: undefined method 'price' for nil:NilClass).

Each EventOption has a price, description, and name that is unique to it, and when I try to access those in multiple ways, I get (NoMethodError: undefined method 'price' for nil:NilClass), for instance.

知道发生了什么吗?

推荐答案

使用 @event_option_id = params[:event_option_id] 您只是将 event_option_id 参数值分配给实例变量.

With @event_option_id = params[:event_option_id] you are just assigning the event_option_id param value to an instance variable.

您应该做的是找到具有 event_option_id 的相应 EventOption 对象,如下所示:

What you should be doing is finding the corresponding EventOption object with the event_option_id as follows:

@event_option = EventOption.find(params[:event_option_id])

使用控制器方法中的那段代码,视图中的 <%= @event_option.price %> 将显示该 EventOption 对象的价格.

With that piece of code in the controller method, <%= @event_option.price %> in the view will show the price of that EventOption object.

这篇关于Rails 中未读取传递的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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