簡體   English   中英

選擇的值在頁面中重置在Rails中提交的紅寶石

[英]choosed values are resetting in page submit in ruby on rails

我不想在提交表單時保留下拉菜單中選中的值並選中單選按鈕。 在兩個單選按鈕的單擊中,我必須提交表單,提交后似乎要重置所選的值。 這是我正在使用的代碼

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

在頁面刷新中選擇值后,將顯示選定的值。但單擊單選按鈕后不顯示選定的值。

將collection_select代碼更改為

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

更新:單選按鈕。

我不確定您如何設置@name但是由於您正在使用它來檢查單選按鈕的狀態,因此您應該在控制器中執行此操作

@name = params[:name]

或者如果您不想這樣做,則可以執行以下操作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM