2016-01-13 102 views
-2

这里是我的博客文章形式嵌入式HTML

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

回答

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

html: => 

应该是这样的

:html => 

当你的键符号(如这里),你可以使用另一种语法哈希

html: { class: 'form-control' } 
0

以及你刚刚调好了两个语法风格

这是老任红宝石语法

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