您将需要 SelectCountryController(或您用来接收所选国家/地区的任何控制器)和您的常规 CountryController。
选择国家控制器:
class SelectCountryController < ApplicationController
def index
if params[:country_id].present?
redirect_to country_path(params[:country_id])
end
end
end
选择国家视图 (app/views/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"