Ruby on rails Rails:如何确定视图中的控制器/操作

Ruby on rails Rails:如何确定视图中的控制器/操作,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,这是我的表格: <%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %> <%= d.label :image, :label => 'Upload logo', :required => false %> <%= d.file_field :image, :label => 'Image, :require

这是我的表格:

<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
    <%= d.label :image, :label => 'Upload logo', :required => false  %>
    <%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px'  %>
    <%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
{:multipart=>true}do | d |%>
“上载徽标”,:必需=>false%>
'Image,:required=>false',:style=>'页边距底部:2px'>
'计费URL',:必需=>false%>
如果操作为“编辑”,则我希望显示以下内容:

<%= f.simple_fields_for :photo, :html => { :multipart => true } do |d| %>
    <%= d.label :image, :label => 'Upload logo', :required => false  %>
    <%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px'  %>
    <%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
{:multipart=>true}do | d |%>
“上载徽标”,:必需=>false%>
'Image,:required=>false',:style=>'页边距底部:2px'>
'计费URL',:必需=>false%>

我怎样才能做到这一点

通常,表单部分仅包含字段,而不包含表单标记或的字段,但如果没有其他方法,则始终可以查看当前设置的
参数[:action]
以及相应的行为。

您可以编写类似的内容

<%- form_url = @object.new_record? ? :photo_attributes : :photo %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
希望这有帮助。

当前页面?(操作:“编辑”)


Rails还可以在视图中使用方法
controller\u path
controller\u name
action\u name

只需在视图中使用@u controller.action\u name即可。

Rails 5:在视图中显示操作

<%= action_name %>
<% if action_name == "edit" %>
  This is an edit action.
<% end %>

视图中的If语句

<%= action_name %>
<% if action_name == "edit" %>
  This is an edit action.
<% end %>

这是一个编辑操作。

这应该是公认的答案--当前页面?太棒了。我见过这样的情况,它不能像预期的那样工作。i、 e.当
controller\u name=='site'
action\u name=='home'
当前页面时?(controller:'site',action:'home')
返回false.Agree。这个答案正是你想要的。小心!当前的页面方法不适用于post请求。例如,想象一下Facebook的登录页面。当您处于登录页面时,在标题上会看到“注册”按钮,当您处于“注册”页面时,它会显示“登录”按钮。如果您使用当前页面方法设计这样的逻辑,那么当您提交登录或注册表单时,页面上会出现两个按钮,并出现错误。所以,页面将再次呈现并出现错误,并且您的方法将无法工作。因此,在本例中,使用如下内容:如果控制器名称='home'&&action\u名称=='login'