您將需要兩個SelectCountryController(或任何控制器,你」重新使用接收選定的國家)和常規CountriesController。
SelectCountryController:
class SelectCountryController < ApplicationController
def index
if params[:country_id].present?
redirect_to country_path(params[:country_id])
end
end
end
選擇國家視圖(app /視圖/ select_country/index.html.erb)
<%= form_tag "", method: :get do %>
<%= collection_select(:country, :country_id, Country.all, :id, :name, prompt: 'Select a Country') %>
<%= submit_tag "Find!" %>
<% end %>
國家控制器:
class CountriesController < ApplicationController
def show
@country = Country.find(params[:id])
end
end
不要忘記以確保您的routes.rb文件中包含正確的路線:
resources :countries
get :select_country, to: "select_country#index"
'country.params [:id]'它是什麼? – IS04 2014-09-22 17:43:21
country.params [:id]只是country_id。 – kongzi 2014-09-23 00:05:50