隐藏的字段没有显示Rails 4

时间:2014-06-07 01:42:28

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.1

Ruby 2.0 Rails 4.1 Windows 8.1

这是我的表格:

<%= bootstrap_form_for(@agent, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10", :multipart => true) do |f| %>
  <%= f.error_summary %>

  <div class="form-inputs">
      <%= f.text_field :first %>
      <%= f.text_field :last %>
      <% f.hidden_field :blah, :value => "hold" %>
      <% f.hidden_field :blah_blah, :value => "hold" %>
      <%= f.file_field :uploaded_file, label: "Image" %>
  </div>
  <p>
    <%= f.form_group do %>
      <%= f.submit class: "btn btn-primary" %>
    <% end %>
  </p>

<% end %>

当我提交表单时,这是我得到的(来自日志文件):

Processing by AgentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"gBOBu9BVo8RQ5UD103KNP1daEvEA76AbhTWbCI1NfM8=", "agent"=>{"first"=>"John", "last"=>"Doe", "uploaded_file"=>#<ActionDispatch::Http::UploadedFile:0x00000006939ff8 @tempfile=#<Tempfile:C:/Users/Mine/AppData/Local/Temp/RackMultipart20140606-6100-13wmcxn>, @original_filename="johndoe.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"lobbyist[uploaded_file]\"; filename=\"johndoe.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create agent"}
blah和blah_blah没有出现。我做错了什么?

2 个答案:

答案 0 :(得分:3)

=之后您需要<%。这告诉rails将其解释为代码。其他=之后您有<%个。

答案 1 :(得分:1)

即使blahblah-blah是隐藏字段,它们仍然必须呈现。区别:

  • <%嵌入式逻辑。这永远不会在视图中呈现HTML。
  • <%=嵌入代码,用于呈现HTML。 Ruby应该在模板中呈现。

它是隐藏的,因为HTML字段类型是hidden,但Ruby仍在视图中嵌入代码。

例如,这个

<%= f.hidden_field :blah, :value => "hold" %>

在浏览器中变成了这个:

<input id="agent_blah" name="agent[blah]" type="hidden" value="hold">

所以,这将有效:

#code above omitted

<div class="form-inputs">
    <%= f.text_field :first %>
    <%= f.text_field :last %>
    <%= f.hidden_field :blah, :value => "hold" %>
    <%= f.hidden_field :blah_blah, :value => "hold" %>
    <%= f.file_field :uploaded_file, label: "Image" %>
</div>

# rest of code