[英]how to get the user_id from a form to the controller in rails
this is how I display the user's email in my dropdown 这就是我在下拉菜单中显示用户电子邮件的方式
<%= collection_select(:user, :id, User.all(:conditions => ["company_name=?", $company]), :user_id, :email, :prompt=>'Select a User') %>
this is the parameters being generated: 这是正在生成的参数:
Parameters: {"commit"=>"Submit", "utf8"=>"✓", "from_date"=>"December 7, 2011 6:45 AM", "to_date"=>"December 9, 2011 7:45 AM", "user"=>{"user_id"=>"12"}}
Once hitting the submit button it has to display the details with the help of user_id and my index function of the controller looks like this 点击提交按钮后,它必须在user_id的帮助下显示详细信息,控制器的索引功能如下所示
def index
if (!params[:user][:user_id].nil?)
user_id = params[:user][:user_id]
else
user_id = current_user.id
end
But I get an error like this: 但我收到这样的错误:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
What could be wrong please help thanks in advance 可能有什么问题,请提前帮助
if params[:user] is nil the "params[:user][:user_id].nil raise exeption" need: 如果params [:user]为nil,则“ params [:user] [:user_id] .nil提高例外”需要:
unless params[:user].nil? and params[:user][:user_id].nil?
....
else
....
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.