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
- Yes. and
#home
is the "action" in PagesController - 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
What Is the Send() Method Used For
How to Access a Ruby Module Method
Rails: How to Determine Controller/Action in View
How to Use Nokogiri to Parse an Xml File
Bundle Install Doesn't Work from Capistrano
Rails Plugin for API Key + Secret Key Signing
How to Debug/Display Request Sent Using Restclient
Why Does Foreman Not Output Some Things Until I Press Control-C
What's the Difference Between Controllers and Actions in Ruby on Rails
Array.Include? Multiple Values
Best Practice About Empty Belongs_To Association
Is Ruby a Scripting Language or an Interpreted Language
How to Install Ruby Gems When Using Rvm
How to Use Rails and Paperclip to Store Photos on Google Cloud Storage
Strange Activerecord::Associationtypemismatch