在rails form_for标签中嵌入了html

时间:2016-01-13 08:33:56

标签: ruby-on-rails

以下是我的博客文章表格

<%= form_for ([@post, @post.comments.build, :html => {:role => 'form'} ])  do |f| %>
  <div class="form-group">
    <%= f.label :name, html: => {:class => 'form-control'} %>
    <%= f.text_field :name, html: => {:class => 'form-control'}  %>
    <%= f.label :body, html: => {:class => 'form-control'} %>
    <%= f.text_area :body, html: => {:class => 'form-control'} %>
   </div>
   <%= f.submit class: "btn btn-primary"%>  
 <%end%>

但我收到此错误,似乎无法弄清楚

[enter image description here]

3 个答案:

答案 0 :(得分:2)

html: =>

应该是这个

:html =>

如果您的键是符号(如此处),则可以使用其他语法进行哈希

html: { class: 'form-control' }

答案 1 :(得分:0)

<%= form_for [@post, @post.comments.build], html: {role: 'form'}  do |f| %>
  <div class="form-group">
     <%= f.label      :name, class: "form-control" %>
     <%= f.text_field :name, class: "form-control" %>
     <%= f.label      :body, class: "form-control" %>
     <%= f.text_area  :body, class: "form-control" %>
  </div>
  <%= f.submit "Submit", class: "btn btn-primary"%>  
<% end %>

答案 2 :(得分:0)

你刚刚混合了两种语法风格

它是旧的ruby语法

:attribute => {value}

或更新的

attribute: {value}

无论如何,您的代码的正确版本是

<%= form_for ([@post, @post.comments.build, html: {role: 'form'} ])  do |f| %>
  <div class="form-group">
    <%= f.label      :name, class: 'form-control' %>
    <%= f.text_field :name, class: 'form-control' %>
    <%= f.label      :body, class: 'form-control' %>
    <%= f.text_area  :body, class: 'form-control' %>
   </div>
   <%= f.submit class: "btn btn-primary"%>  
 <%end%>