Rails: How to Determine Controller/Action in View

Rails: How to determine controller/action in view

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.

Short way to check for controller/action in view

You can always define a custom method in your ApplicationController. For simple cases, could be something like:

def couple?(route_path)
controller, action = route_path.split('#')
controller_name == controller && action_name == action
end
helper_method :couple?

This way, you are able to use that method in your controllers and views. If you don't really need to use it in your controllers, define it directly into ApplicationHelper.

Can I get the name of the current controller in the view?

controller_name holds the name of the controller used to serve the current view.

How do I get the current action from within a controller?

Controllers have action_name method/accessor (defined in AbstractController::Base for current rails, ActionController::Base for older)

what determines controller action ruby rails

  1. Yes. and #home is the "action" in PagesController
  2. You get a uninitialized constant PagesController error

So, your controllers should always be in the form NameController defined in name_controller.rb, and actions as public methods in NameController.

Call custom controller action from view when I click on a button Rails

Your action is private. Move it above the private line and it will work

How to determine which view to render in a controller action

Not sure if this is feasible without seeing your code, but how about something like this:

respond_to do |format|
format.html { render(:action => 'new') }
format.widget { render(:action => 'widget_login') }
end

Then in your widget link to new.widget.

In Rails, how can you output the name of the action from inside the view?

params[:action]

you may need to update your render line to

render :template => "ctrl/update"


Related Topics



Leave a reply



Submit