Adding an action to an existing controller (Ruby on Rails)
Your routing isn't set up to allow that route. Assuming you're using the default scaffolding, put this line before the map.resources :posts
line in config/routes.rb:
map.connect "posts/:action", :controller => 'posts', :action => /[a-z]+/i
The regex for :action
restricts it to just a-z (to avoid catching things like /posts/1). It can be improved if you need underscores or numbers in your new actions.
How to add a new action to the existing controller?
Add a new action is simple. All you have to do is add a method on your controller, like, for example:
# app/controllers/dummy_controller.rb
def get_back
logger.warn "It works!"
redirect_to :back
end
Now, to be able to access this action throgh a URL, you need to have a route for that. This is done in your config/routes.rb
file. You can add it as a hard route, like
get '/go_back', to: "dummy#get_back"
This is the simplest possible route. But you might want it to behave like a restful route. This is useful if you are doing an action over one or more models. So in your route file, you will have something like this:
resources :dummy do
collection do
get 'get_back'
end
end
This allows you to accept a get
method over a collection. You will have the helper dummy_go_back_url
, and to get to this page the url is /dummies/go_back
.
This is for acting over a collection of resources. If you are acting on one specific object, you should specify a member
action:
resources :dummy do
member do
get 'get_back'
end
end
Since a member action is for only one object, you will have a url like /dummies/123/go_back
. This automatically will set the variable params[:id]
in your controller to 123
, allowing you to easily fetch your object. Also, the helper method dummy_go_back_path
is defined, and received one object or id as parameter to generate the correct url.
These are the most simple routes you can have, but you can look in routing outside in from rails guides as a reliable source of information.
Adding new action to an existing controller
In your routes
resources :ControllerName do
collection do
get :NewActionName
post :NewActionName
end
end
Make changes like this and it should work
Create a new action for existing controller
I'm pretty sure you won't be able to do this in a 100% automated way. The reason is that Rails doesn't know what you've done with your routes or controller, and it would require some logic to know how to update these existing files. Your best bet is to just add the new action manually. Add the new method to your controller, update your routes file, and add the view. It will probably take 1 minute at most. Also, if you're not using version controller (which your question eluded to), then you don't have to worry about it automatically overwriting something.
adding a method to an existing controller
I think currently you have this in routes
resources :products do
end
just replace this with
resources :products do
get :help, :on => :member, :as => :help
end
And add method in controller and add view named help.erb.html(if you r using erb) in views/product folder.
you can use help_path and help_url
One controller, two edit actions
You have a few simple changes to make. First, in your routes.rb
file, you need to add the new route. The easiest way is this:
resources :courses do
member do
get "editclean"
end
end
This will add an "editclean" route and automatically editclean
action in your controller.
In the controller, you have the add the editclean
action to your before_action handler:
before_action :set_course, only: [:show, :edit, :editclean, :update, :destroy]
You also have to create the view file editclean.html.erb
. Your edit
action probably shares a _form.html.erb
partial with the new
action. You can either add conditionals to this partial or copy the relevant bits to the new view file. Here's what your new view might look like:
<h2>Edit (clean) Course</h2>
<%= form_for @course do |f| %>
<%= f.label :title, "Title" %>
<%= f.text_field :title %>
<%= f.label :start_date, "Start Date" %>
<%= f.text_field :start_date %>
<%= f.label :end_date, "End Date" %>
<%= f.text_field :end_date %>
<%= f.submit "Save" %>
<% end %>
Note that this will re-use the existing update
action to save the fields. If you want special behavior (avoiding some data checks), you might want to implement an alternative action, but the basic update
action frequently works exactly as expected.
When you link to this you can use the route helper that was automatically created for you, like so:
<%= link_to "Edit (clean)", editclean_course_path(@course) %>
And that's about all there is to adding a new action.
Add new action to route
in your config/routes.rb file do this
resources :users do
collection do
get 'another_new'
post 'another_create'
end
end
Also have a look HERE for clear understanding of concepts.
Hope this helps you dude :)
Related Topics
How to Compare Strings Ignoring the Case
Given an Array of Arguments, How to Send Those Arguments to a Particular Function in Ruby
Ruby: Dynamically Generate Attribute_Accessor
Redefining a Single Ruby Method on a Single Instance with a Lambda
Using Join Tables in Ruby on Rails
How to Find the Namespace/Module Name Programmatically in Ruby on Rails
Colon (:) Appears as Forward Slash (/) When Creating File Name
Error: SASS Installation for Windows
How to Configure Ruby on Rails with Oracle
How to Count the Number of Records That Have a Unique Value in a Particular Field in Ror
How to Select Every Nth Item in an Array
Prevent Rails Test from Deleting Seed Data
Installing Jekyll on Ubuntu 14.04
Couldn't Require Openssl in Ruby