9

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

3 Answers 3

17

You should have a end for 'do'

<% if @commentable == @user %>
  <%= semantic_form_for [@commentable, @comment] do |f| %>
  <% end %>
<% else %>
  <%= semantic_form_for [@user, @commentable, @comment] do |f| %>
  <% end %>
<% end %>

It is expecting 'end' after 'do' not 'else'.

Thanks

2
  • 1
    So what is best way to solve this without needing to have the whole form_for code twice?
    – Robert
    Commented Sep 12, 2012 at 18:40
  • I am not pretty much experienced with semantic_form_for, so can't say, but any way whatever you do, one iteration will be there. Commented Sep 12, 2012 at 18:42
9

It's mad because the do bit starts a block, which expects an end to end it. But when the condition is true, it finds an else instead. And note that if the condition was false, it would find an end like it wants - but not the end you want! It would find the end that ends your if statement - not the end that you want to end your block.

If your semantic_form_for blocks 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 single semantic_form_for:

<% args = (@commentable == @user)? [@commentable, @comment] : [@user, @commentable, @comment] %>
<%= semantic_form_for(args) do |f|
    Whatever...
<% end %>

Hope that helps!

1
  • Thanks, this fixed it! And it looks way cleaner than the if statement I was trying too.
    – Robert
    Commented Sep 12, 2012 at 18:50
3

The problem you're describing is plain Ruby, unrelated to view. you can write something like this instead:

<%
if @commentable == @user
  args = [@commentable, @comment]
else
  args =  [@user, @commentable, @comment]
end
%>
<%= semantic_form_for args do |f| %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.