开发者

Using submit_tag to declare format

I'm writing a rails app where users generate markers on a google map and then have the option to download them as .kml files. Thing is, I'm adding a feature to change the map to where they can see when they added specific markers to the map, with intervals. I want to use the same form as I did to previously download the .kml files but also add an extra s开发者_如何学编程ubmit button that will not do anything but run some controller logic. I originally had:

<%= form_tag customMapGenerate_path(@device, :format => 'kml'),  :method => :get do %>

for my form_tag

How can I modify my two submit links:

<%= submit_tag 'Download KML' %>
<%= submit_tag 'Display on map' %>

to render KML and then not render anything (and stay on page) for both of the conditions below:

if(params[:commit] == "Download KML")
  respond_to do |format|
    format.kml
  end
  return
elsif(params[:commit] == "Display on map")
  //simple ruby code
  return
end


You can use button_tag

Remove the format from the form_tag and it should give you something like this

<%= form_tag customMapGenerate_path(@device),  :method => :get do %>
 // your form here
 <%= button_tag 'Download KML', value: 'kml', name: 'format' %>
 <%= button_tag 'Display on map', value: 'html', name: 'format' %>
<% end %>

Then in you controller, you simply use the respond_to do |format| to differenciate between your two types of response.


You can set the respond headers in your action to force the browser to download a file

response.headers['Content-Type'] = 'application/vnd.google-earth.kml+xml'
response.headers['Content-Disposition'] = 'attachment; filename=map.kml'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜