How reliable is the action_name method in rails?

How can I ensure that the controller.action_name will return the correct information? I rely on this to create user friendly messages in the menu bar. Consider the following scenario.

The action_name method works great when I use:

redirect_to edit_profile_path # I get action_name as "edit" in my ApplicationController


But that doesn't provide the information I need when I use the following:

render :action => "edit"  # The action_name shows as "get" instead of "edit"


Is there a way that I can use the render action and still get the correct action name?

Thanks, Tabrez

---- code snippets in response to Mischa's comment ----

This is what I have in the controller:

def update
    if @profile.update_attributes(params[:profile]) 
        format.html { 
  [:notice] = 'Profile was successfully updated.'            
            render :action => "edit" 


I am not redirecting because I still want the user to stay in edit mode.

In my application helper, I have code for the following effect:

if(action_name == "edit" && (can? :read, l_disp_object) )  then
    path_links_subheader.concat(link_to 'Show Saved Version', :controller => controller.controller_path, :action => :show) 


Basically, if the user is in edit mode, I want the menu to display a Show Saved Version link. But when I use the "render: action" syntax the action changes to "get", which makes sense depending on what Misha mentioned (the action is wrong here). So, is there a way I can do this without triggering redirects? The only reason I'm not using redirection is because the logic to keep the current tab for the user works with the render action. I might have to rewrite it if the only possible way to do this is by redirecting.


source to share

1 answer

The naming is a little confusing, but it render :action => "edit"

reflects the kind of editing. It has nothing to do with editing the controller action. action_name

is reliable, but for this reason you cannot rely on it. Open a new question and / or share another code for suggestions on how to fix your problem.



All Articles