我如何从collection_select表单中获取id?

时间:2011-10-21 19:28:54

标签: ruby-on-rails ruby-on-rails-3 forms ruby-on-rails-3.1

我可以在日志中看到参数里面的id,但是我无法在控制器中访问它。可以告诉我我还能怎么办?显然 @city=City.find(params[:cities][:city_id])没有做好这件事。谢谢你

  <%= form_for :city, :url=>{:action =>"next"} do |f| %>
  <%= f.collection_select(:city_id, City.all, :id, :name ,:prompt=>"Select your city") %>

   <%=f.submit "continue" %>
   <%end%>

家庭控制器

       def next
       @city=City.find(params[:city_id]) 
       session[:city_id] = @city.id
       redirect_to :controller=>"parks",:action =>"show" 
       end

在日志中

     Started POST "/home/next" for 127.0.0.1 at 2011-10-21 12:16:37 -0700
     Processing by ParkController#show as HTML
     Parameters: {"utf8"=>"✓",  "authenticity_token"=>"7VVJ9GHcU4miYLCkSt91S674GGTScor86Tcsz7O25ik=", "city_id"=>"2",  "commit"=>"continue"}
     Rendered park/show.html.erb within layouts/header (2.5ms)
     Completed 500 Internal Server Error in 7ms

3 个答案:

答案 0 :(得分:1)

你写了

<%= collection_select(nil,:city_id, City.all, :id, :name ,:prompt=>"Select your city") %>

但你应该写

<%= f.collection_select(:city_id, City.all, :id, :name ,:prompt=>"Select your city") %>

就像你为提交按钮所做的那样!实际上,您也可以将此行添加到表单目标:

<%= params.inspect %>

查看哪些值在哪个散列中传输。

答案 1 :(得分:1)

嗯,也许我错了(不是经验丰富的Rails开发者),但是你不必写完

params[:city_id]

答案 2 :(得分:1)

嗯该死的我是一个白痴一开始没看到它。你写了

@city=City.find(params[:cities]) 

但它应该是

@city=City.find(params[:city_id])