简体   繁体   中英

choosed values are resetting in page submit in ruby on rails

I don't want to rest the selected values from drop down and checked radio button while submitting the form. In the onclick of both radio button i have to submit the form,after submitting it appears to reset the selected values. Here is the code am using

 <%= form_tag(:action =>"show", :method => "post") do %>
    <%= @name %>.
    <strong>Select device: </strong> <%= collection_select(:device, :id, @devices, :id, :name, options ={:selected => params[:name],:prompt => "-Select a device"}) %>
    <br></br>
    <strong>Chose: </strong>
    <%=  radio_button_tag :name,:time,@name.eql?('time'),:onclick => "this.parentNode.submit();"%>Time

    <%=  radio_button_tag :name,:graph,,@name.eql?('graph') :onclick => "this.parentNode.submit();" %>Graph
    <%end%>

After choosing the values in page refresh selected values are showing.But not displaying selected values after the radio button click.

change the collection_select code to

collection_select(:device, :id, @devices, :id, :name, { selected: params.fetch(:device, {})[:id].to_i, :prompt => "-Select a device" }) %>

UPDATE: for the radio buttons.

I'm not sure how you are setting @name but since you are using that to check the state of the radio button, you should be doing this in the controller

@name = params[:name]

or if you don't want to do that, you can do the following

<%= params[:name] %>.
<%= radio_button_tag :name, :time, params[:name] == 'time', onclick: 'this.parentNode.submit();' %>Time
<%= radio_button_tag :name, :graph, params[:name] == 'graph'), onclick: 'this.parentNode.submit();' %>Graph

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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