Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 下拉选择器,选定选项_Ruby On Rails_Select - Fatal编程技术网

Ruby on rails 下拉选择器,选定选项

Ruby on rails 下拉选择器,选定选项,ruby-on-rails,select,Ruby On Rails,Select,我使用的是RubyonRails版本3。我为选择器定义了以下帮助器方法: def current_user_selector collection_select(:user, :id, User.all, :id, :name, {:prompt => "Select a User"}); end 我在我的index.html.erb中引入了上述选择器: ... <%= current_user_selector %> ... 我已经在我的控制器的几个动作中

我使用的是RubyonRails版本3。我为选择器定义了以下帮助器方法:

  def current_user_selector
    collection_select(:user, :id, User.all, :id, :name, {:prompt => "Select a User"});
  end
我在我的
index.html.erb
中引入了上述选择器:

...
<%= current_user_selector %>
...
我已经在我的控制器的几个动作中添加了上面的一行,但是我不断得到异常

目前,我的行动如下:

  # GET /users/:id/click
  # GET /users/:id/click.xml
  def click
    @user = User.find(params[:id])
    @users_to_click = User.where("clicks_given - clicks_received >= ?", -5).order("clicks_given - clicks_received")
    selected_user = params[user][id]

    respond_to do |format|
      format.html # click.html.erb
      format.xml  { render :xml => @user }
    end
  end
我的问题是,我如何获得下拉菜单的选定用户

Naren Sisodiya给我带来了以下例外:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

params[:user][:id]将为您提供所选用户的id,因此您需要找到使用此id的用户

selected_user = User.find(params[:user][:id])
编辑:

还要确保您是以params[:user][:id]而不是params[user][id]的身份访问它

更新:

您需要将所选的值传递给服务器,正如您提到的,您有一个按钮可以调用单击操作。现在,您可以使用下拉菜单和一个提交按钮创建一个from;像

<%= form_tag('/click',:method=>'get') do -%>
 <%= current_user_selector %>
 <%= submit_tag 'Get Selected' %>
<% end -%>
'get')do-%>

它不起作用。有关返回的错误,请参阅上面的问题。如果下拉列表是在索引文件中呈现的,那么如何将其选定值传递给服务器?您需要将其传递给服务器。当你的点击动作被调用的时候?在索引文件上我有一个按钮来调用点击动作。如何将所选值传递给服务器?您可以创建一个表单,其中包括下拉列表和您的按钮,该按钮将是表单的提交按钮。表单动作应该与你的点击动作相映射。又是我,难道不可能用JS生成下拉菜单并用JS获取所选用户吗?避免表单提交。这是因为当前用户还被另一个调用单击操作的对接使用。我已将我的应用程序上载到github(),以便您可以查看正在使用的内容。您可以将表单放在使用下拉列表的位置吗?下拉菜单正在
index.html.erb
中呈现。这是个问题。
<%= form_tag('/click',:method=>'get') do -%>
 <%= current_user_selector %>
 <%= submit_tag 'Get Selected' %>
<% end -%>