Rails if they are still visible

My comment controller needs to be configured for nesting, but I am getting a few errors. Here is what I tried:

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

What 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 is not working? It seems simple enough ...

Here is 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 %>
+5
source share
3 answers

This is crazy because a bit dofires a block that expects it to endfinish. But when the condition is true, it finds else. And note that if the condition were false, he would find endhow he wants, but not endwhich you want! It will find endwhich one your statement will complete if, and not endwhich you want to complete.

semantic_form_for , Paritosh. , , , semantic_form_for:

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

, !

+7

'do'

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

"" "" "".

+11

The problem you are describing is simple Ruby, not presentation related. you can write something like this:

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

All Articles