32

This is my form partial:

<%= 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 %>

If the action is edit I want to show this instead:

<%= 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 %>

How can i achieve this?

Larry K
  • 47,808
  • 15
  • 87
  • 140
Rails beginner
  • 14,321
  • 35
  • 137
  • 257

5 Answers5

117

current_page?(action: 'edit')

See ActionView::Helpers::UrlHelper#current_page?

Rails also makes the methods controller_path, controller_name, action_name available for use in the view.

fny
  • 31,255
  • 16
  • 96
  • 127
  • 4
    This should be the accepted answer--`current_page?` is awesome. – thekingoftruth May 14 '14 at 20:20
  • 7
    Hmm. I've seen cases where it doesn't work as expected. i.e. when `controller_name == 'site'` and `action_name == 'home'` but `current_page?(controller: 'site', action: 'home')` returns false. – thekingoftruth Jul 16 '14 at 07:35
  • Agree. This answer is exactly what you're looking for. – João Apr 10 '15 at 08:49
  • Heads up! current_page method doesn't work with post request. For example, imagine Facebook's /login page. When you are in login page, on the header you see sign up button and when you are in sign up, it displays login button. If you design such a logic by using current page method, you will end up with two buttons on the page, when you submit login or sign up form and get error. So, page will render again with an error and your method will not work. So, in this case use something like this: if controller_name == 'home' && action_name == 'login' – cyonder Nov 07 '15 at 09:45
31

Generally the form partial only contains the fields, not the form tag or the fields for, but if you have no other way, you can always see what params[:action] is currently set to and behave accordingly.

tadman
  • 208,517
  • 23
  • 234
  • 262
9

Rails 5: Display Action within the view

<%= action_name %>

If statement within the view

<% if action_name == "edit" %>
  This is an edit action.
<% end %>
thedanotto
  • 6,895
  • 5
  • 45
  • 43
9

You could write something like

<%- form_url = @object.new_record? ? :photo_attributes : :photo %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>

That is, if you have an @object to check against. Otherwise you could use action_name (and even controller_name).

So something like:

<%- form_url = action_name == :edit ? :photo : :photo_attributes %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>

Hope this helps.

nathanvda
  • 49,707
  • 13
  • 117
  • 139
0

Just use @_controller.action_name in view

Arugin
  • 1,931
  • 2
  • 13
  • 17