My comment controller needs adjust for nesting but I’m getting a few errors. Here’s what I’ve been trying:
<% if @commentable == @user %>
<%= semantic_form_for [@commentable, @comment] do |f| %>
<% else %>
<%= semantic_form_for [@user, @commentable, @comment] do |f| %>
<% end %>
Which gives this:
/Users/rbirnie/rails/GoodTeacher/app/views/comments/_form.html.erb:3: syntax error, unexpected keyword_else, expecting keyword_end'); else
Any idea why this isn’t working? Seems simple enough…
Here’s the full view:
<% 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 %>
It’s mad because the
dobit starts a block, which expects anendto end it. But when the condition is true, it finds anelseinstead. And note that if the condition was false, it would find anendlike it wants – but not theendyou want! It would find theendthat ends yourifstatement – not theendthat you want to end your block.If your
semantic_form_forblocks have different contents in each case, use Paritosh’s answer. But if they’re the same code and you want to avoid repeating it, you can pick the arguments conditionally, and then pass them into a singlesemantic_form_for:Hope that helps!