Ruby-on-rails Rails 如果有其他视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12394258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails if else in view
提问by Robert
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 %>
采纳答案by Xavier Holt
It's mad because the dobit starts a block, which expects an endto end it. But when the condition is true, it finds an elseinstead. And note that if the condition was false, it would find an endlike it wants - but not the endyou want! It would find the endthat ends your ifstatement - not the endthat you want to end your block.
这很疯狂,因为该do位开始了一个块,它期望 anend结束它。但是当条件为真时,它会找到一个else替代。并注意,如果条件为假,它会找到end它想要的 - 但不是end你想要的!它会找到end结束你的if语句的end那个- 而不是你想要结束你的块的那个。
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 single semantic_form_for:
如果您的semantic_form_for块在每种情况下都有不同的内容,请使用 Paritosh 的答案。但是如果它们是相同的代码并且您想避免重复它,您可以有条件地选择参数,然后将它们传递给单个semantic_form_for:
<% args = (@commentable == @user)? [@commentable, @comment] : [@user, @commentable, @comment] %>
<%= semantic_form_for(args) do |f|
Whatever...
<% end %>
Hope that helps!
希望有帮助!
回答by Paritosh Singh
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
谢谢
回答by davidrac
The problem you're describing is plain Ruby, unrelated to view. you can write something like this instead:
您描述的问题是普通的 Ruby,与视图无关。你可以写这样的东西:
<%
if @commentable == @user
args = [@commentable, @comment]
else
args = [@user, @commentable, @comment]
end
%>
<%= semantic_form_for args do |f| %>

