简体   繁体   English

Rails新手:集合选择和控制器逻辑

[英]Rails Newbie: Collection Select & Controller Logic

I've read many related questions here, but I still don't understand how to do the following: I have a "Country" model and I'd like to create a select form that will allow users to select any of the existing countries in the model, and be redirected to that country's "show" page. 我在这里阅读了许多相关的问题,但是我仍然不知道如何执行以下操作:我有一个“国家/地区”模型,我想创建一个选择表单,该表单将允许用户选择任何现有的国家/地区在模型中,然后重定向到该国家/地区的“显示”页面。

My collection_select logic is: 我的collection_select逻辑是:

<%= collection_select(:country, :country_id, Country.all, :id, :name, prompt: 'Select a Country') %>

<%= submit_tag "Find!", redirect_to (country.params[:id])%>

Any help would be appreciated! 任何帮助,将不胜感激!

Rails uses MVC, so all logic should be in model(skinny controllers, fat models) and you should select your countries something like this @country = Country.find(params[:country_name]) . Rails使用MVC,因此所有逻辑都应该在模型中(瘦控制器,胖模型),并且应该选择类似@country = Country.find(params[:country_name]) And then in view its gonna be <%= submit_tag "Find!", redirect_to country_show_path(@country) %> . 然后鉴于其将是<%= submit_tag "Find!", redirect_to country_show_path(@country) %> If I understood your question right this is the answer. 如果我正确理解您的问题,这就是答案。

Select form 选择表格

Create a drop down within your form: 在表单中创建一个下拉列表:

<%= form_tag countries_path, method: :get do %>
    <%= collection_select(:country, :country_id, Country.all, :id, :name, prompt: 'Select a Country') %>
<%= submit_tag %>

In this case I am hitting contries_path and I have specified a GET request. 在这种情况下,我遇到的是contries_path并且指定了GET请求。 The value selected by the form will be passed to CountriesController#show . 表单选择的值将传递给CountriesController#show

Post to controller 发布到控制器

You can find the country, using the value passed to the form, via the params hash: 您可以使用传递给表单的值通过params哈希找到国家/地区:

class CountriesController < ApplicationController
  def show
    @country = Country.find(params[:country][:country_id])
  end
end

You will need both SelectCountryController (or whatever controller you're using to receive the selected country) and your regular CountriesController. 您将同时需要SelectCountryController(或用于接收所选国家/地区的任何控制器)和常规的CountrysController。

SelectCountryController: SelectCountryController:

class SelectCountryController < ApplicationController
  def index
    if params[:country_id].present?
      redirect_to country_path(params[:country_id])
    end
  end
end

Select country view (app/views/select_country/index.html.erb) 选择国家/地区视图(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 %>

Countries Controller: 国家主管:

class CountriesController < ApplicationController
    def show
      @country = Country.find(params[:id])
    end
end

Don't forget to make sure you have the proper routes in your routes.rb file: 不要忘记确保您的route.rb文件中具有正确的路由:

resources :countries
get :select_country, to: "select_country#index"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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