How to specify a response format based on a form option in Rails 3.0.x

Environment: Rails 3.0.4 and Ruby 1.9.2

I have the following form:

<%= form_tag( {:action => 'show', :format => :pdf}, :method => :post) do %> .. list of items ... <%= submit_tag "Show", :onclick => "return checkAllFields(4);", :remote => true %> <select name="format"> <option name="HTML">HTML</option> <option name="PDF">PDF</option> </select>) <% end %> 

As you can see, I have specified a format that will be "pdf" in the URL. I want to request an HTML or PDF response from a controller based on a selection option. Both requests work individually, that is, I can display HTML or PDF, but I can not make it a dynamic user choice. (I can't even get it to work with two separate hard-coded buttons)

Controller code obviously

 def show # code to locate items here respond_to do |format| format.html format.pdf { render :layout => false } prawnto :filename => "list.pdf", :prawn => { } end end 
+4
source share
1 answer

I would try the following:

First, you may need to remove hardcoded :format => :pdf from the form tag (since it can override this parameter below).

Then make sure the select tag passes the correct values. There is an assistant that you can use:

 select_tag :format, options_for_select([["HTML", "html"], ["PDF", "pdf"]], "html") 

which returns something like the following HTML:

 <select id='format' name='format'> <option value='html' selected='selected'>HTML</option> <option value='pdf'>PDF</option> </select> 
+7
source

Source: https://habr.com/ru/post/1341906/


All Articles