I have a form which has a select box with name of cities from the database. When i select a city and click continue, i want it to grab the city_id
selected and redirect me to another page (:controller=> 'people', action => 'index')
where i am going to make use of that city_id
. How do i do that?Iam using RAILS 3.This is what i have..
View
<%= form_for :c开发者_StackOverflow社区ities, :url=>{:action =>"next"} do |f| %>
<%= collection_select(nil, :city_id, City.all, :id, :name ,:prompt=>"Select your city") %>
<%=f.submit "continue" %>
<%end%>
Controller
class HomeController < ApplicationController
def next
@city = City.find(params[:city_id])
redirect_to :controller => "people", :action => "index"
end
def new
@city = City.new
end
end
People Controller
class PeopleController < ApplicationController
def index
@city = City.find(params[:city_id])
end
end
You can do this by setting a session variable in your next
action:
class HomeController < ApplicationController
def next
@city = City.find(params[:city_id])
session[:city_id] = @city.id
redirect_to :controller => "people", :action => "index"
end
def new
@city = City.new
end
end
class PeopleController < ApplicationController
def index
unless session[:city_id].nil? || session[:city_id].blank?
@city = City.find(session[:city_id])
# do your stuff here
end
end
end
You can also do this with a flash variable, but session is likely the proper place.
精彩评论