Supersonic
Supersonic

Reputation: 430

Rendering a view depending on the select tag value - Simple

I have a view like this:

<h2>Select the type of Project</h2>
<%= form_tag('/select') do -%>
<label>Select A Type Of Project</label><%= select_tag "project_type", options_from_collection_for_select(ProjectType.all, "name", "name"),:prompt => "Select Project Type" %> 
<%= submit_tag 'Next' %>
<% end -%>

Here I am just selecting a specific type of project and submitting.

The HTML Output is:

<select id="project_type" name="project_type">
<option value="">Select Project Type</option>
<option value="Sales">Sales</option>
</select>

Now, I want to render a specific template depending on the type of project selected. If the value = "Sales" I would like to render the template sales.html.erb.

What should I put in the controller action named select. Rite now its empty.

Thanks :)

Upvotes: 0

Views: 723

Answers (2)

shivashankar
shivashankar

Reputation: 1177

Under your action add this

template = params[:project_type] || 'new'
respond_to do |format|
  format.html { render template }
  format.json { render json: @projects }
end

'new' method is assumed default method

Upvotes: 2

Pavel S
Pavel S

Reputation: 1543

Something like that

def select
  view_to_render = case params[:project_type]
  when 'name1'
      #do smth
      'view_name'
  else
      # do smth else
      'another_view'
  end   
  respond_to do |format|
    format.html { render view_to_render }
  end
end

Upvotes: 2

Related Questions