简体   繁体   中英

How do i grab an id from a collection_select form?

I can see the the id inside the parameters in the log but i cant access it in the controller.Can any body please show me how else i could do?Apparently @city=City.find(params[:cities][:city_id]) is not doing the job.Thank you

  <%= 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%>

Home controller

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

In the log

     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

You wrote

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

But you should write

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

Like you did for the submit button! Actually you can also add this line to the forms target:

<%= params.inspect %>

to see which values are transfered in which hash.

Umm, maybe I am wrong (not an experienced Rails dev), but don't you have to just write

params[:city_id]

?

Awww damn Im an idiot didnt see it in the beginning. You wrote

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

But it should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM