基于 Rails 4 中新建和编辑表单的控制器操作的条件 [英] conditional based on the controller action for new and edit forms in Rails 4
本文介绍了基于 Rails 4 中新建和编辑表单的控制器操作的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在新视图和编辑视图中呈现了一个表单:
I have a form rendered in the new and edit views:
<%= simple_form_for(@course, html: {class: "form-horizontal"}) do |f| %>
由于我的编辑表单应该略有不同:simple_form_for([@category, @course])
,有没有办法根据控制器操作(编辑或新建)编写条件?
Since my edit form should be slightly different: simple_form_for([@category, @course])
, is there a way to write a conditional based on the controller action (edit or new) ?
# if controller action is new
<%= simple_form_for(@course, html: {class: "form-horizontal"}) do |f| %>
#if controller action is edit
<%= simple_form_for([@category, @course], html: {class: "form-horizontal"}) do |f| %>
推荐答案
您可以使用 action_name
<% if action_name == 'edit' %>
#
<% else %>
#
<% end %>
或者为了真正安全,也使用 controller_name
Or to be really safe, use controller_name also
<% if controller_name == 'cources' && action_name == 'edit' %>
这篇关于基于 Rails 4 中新建和编辑表单的控制器操作的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文