0

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(:country, :country_id, Country.all, :id, :name, prompt: 'Select a Country') %>

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

Any help would be appreciated!

kongzi
  • 5
  • 3

3 Answers3

0

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]). And then in view its gonna be <%= submit_tag "Find!", redirect_to country_show_path(@country) %>. If I understood your question right this is the answer.

Oleksandr Verhun
  • 814
  • 1
  • 8
  • 23
0

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. The value selected by the form will be passed to CountriesController#show.

Post to controller

You can find the country, using the value passed to the form, via the params hash:

class CountriesController < ApplicationController
  def show
    @country = Country.find(params[:country][:country_id])
  end
end
Tom Kadwill
  • 1,448
  • 3
  • 15
  • 21
  • Tried this, but I'm getting an error - "undefined method `[]' for nil:NilClass" - the error is on the controller line: @country = Country.find(params[:country][:country_id]) – kongzi Sep 22 '14 at 19:25
  • Is there anything in `params`? Try printing out `params` and see whether there is anything in there for `country` or `country_id` – Tom Kadwill Sep 22 '14 at 19:39
  • Parameters: {"id"=>"1"} – kongzi Sep 22 '14 at 19:59
  • So is 1 the correct country_id? You can access it via `params[:id]` – Tom Kadwill Sep 22 '14 at 20:04
  • 1 is the correct country_id. It is showing up now, but when I hit the submit button, the URL reloads but remains on the main 'index' page - 'http://localhost:3000/countries?utf8=%E2%9C%93&country%5Bcountry_id%5D=2&commit=Save+changes' – kongzi Sep 28 '14 at 00:00
  • The action that the form posts to can be dictated by the url action specified at the top of the form, [See example here](http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object). Also, you probably don't want to do a `redirect_to` in the submit tag. The form should take care of this automatically, if set up correctly – Tom Kadwill Sep 28 '14 at 10:34
0

You will need both SelectCountryController (or whatever controller you're using to receive the selected country) and your regular CountriesController.

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)

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

resources :countries
get :select_country, to: "select_country#index"
Marcelo Ribeiro
  • 1,718
  • 1
  • 13
  • 27