Hidden field in rails form

不羁岁月 提交于 2019-12-20 09:30:06

问题


I have this form in a view in my project. I need to pass the task_id to a certain controller, but the log does not seem to be receiving the parameters. I don't know what the problem is.

<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
  <%f.hidden_field :task_id, :value => task.id%>
  <td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>

回答1:


You are missing on = after <%. The equal sign is needed whenever you want to the result appears on the HTML, so it is used with the field tags methods or render, for instance. You should not use the equal when using a if, for example, because this is not what you want to print (well, it can be, but most likely it isn't)

<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
  <%= f.hidden_field :task_id, :value => task.id%>
  <td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>

However, as @AntonGrigoriev pointed out, you should use a object if you have, like this

<%= form_for @task, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>

or you can simply use the hidden_field_tag

<%= hidden_field_tag :task_id, task.id %>



回答2:


Hi please test with following code to send hidden value in rails, I have tried and worked for one of my application :

hidden_field_tag(name, value = nil, options = {}) public eg:

<%= hidden_field_tag(:field_name,value=@offer_status)%>


来源:https://stackoverflow.com/questions/16280341/hidden-field-in-rails-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!