Ruby on rails 若有,;else语句更改_form.html.erb-Rails4中的提交按钮文本
请告诉我这是如何正确书写的: 文件:views/advists/_form.html.erbRuby on rails 若有,;else语句更改_form.html.erb-Rails4中的提交按钮文本,ruby-on-rails,ruby-on-rails-4,control-flow,Ruby On Rails,Ruby On Rails 4,Control Flow,请告诉我这是如何正确书写的: 文件:views/advists/_form.html.erb <% if action: "new" %> <div class="form-actions"> <%= f.button :submit, 'Post Job' %> </div> <% else %> <div class="form-actions"> <%= f
<% if action: "new" %>
<div class="form-actions">
<%= f.button :submit, 'Post Job' %>
</div>
<% else %>
<div class="form-actions">
<%= f.button :submit, 'Update Job' %>
</div>
<% end %>
您要使用的。此外,使用三元运算符(一行),而不是if/else
:
甚至更短:
<%= f.submit action_name == 'new' ? 'Post Job' : 'Update Job'%>
建议检查
f.object.persistend?
<如果创建时验证失败,代码>操作\u name=='new'将返回false。
<%= f.submit action_name == 'new' ? 'Post Job' : 'Update Job'%>