我的評論控制器需要調整嵌套,但我收到一些錯誤。這是我一直想:Rails if else in view
<% if @commentable == @user %>
<%= semantic_form_for [@commentable, @comment] do |f| %>
<% else %>
<%= semantic_form_for [@user, @commentable, @comment] do |f| %>
<% end %>
其中給出這樣的:
/Users/rbirnie/rails/GoodTeacher/app/views/comments/_form.html.erb:3: syntax error, unexpected keyword_else, expecting keyword_end'); else
知道爲什麼這是不工作?似乎很簡單...
下面是完整的視圖:
<% if @commentable == @user %>
<%= semantic_form_for [@commentable, @comment] do |f| %>
<% else %>
<%= semantic_form_for [@user, @commentable, @comment] do |f| %>
<% end %>
<div class="control-group">
<%= f.label :subject %>
<div class="controls"><%= f.text_field :subject %></div>
</div>
<div class="control-group">
<%= f.label :body %>
<div class="controls"><%= f.text_area :body, rows: 8 %></div>
</div>
<div class="form-actions">
<%= f.submit "Submit", :class => "btn-success" %>
</div>
<% end %>
如果你是自由選擇的模板處理程序,看看Haml的(http://haml.info/docs/yardoc/file.HAML_REFERENCE.html)或苗條(http://slim-lang.com/)。這兩種語言都讓你有機會忘記關閉塊/ ifs並使你的模板更具可讀性。 – jdoe