Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new action to route

I got these actions in users controller

class UsersController < ApplicationController
  def index #default action
    ...
  end

  def new #default action
    ...
  end

  def another_new
    ...
  end

  def create
    ...
  end

  def another_create
    ...
  end
end

I want to be able to /users/another_new and call from some sort of link :method => :another_create to make /users/another_new

I got the following config/routes.rb

get '/users/another_new' :to => 'users#another_new'
resources :users

my question is if this is the correct way to add the get and how I add the another_create method.

like image 829
user1842826 Avatar asked Nov 21 '12 17:11

user1842826


People also ask

How do I add actions to my controller?

Adding an Action to a Controller You add a new action to a controller by adding a new method to the controller. For example, the controller in Listing 1 contains an action named Index() and an action named SayHello(). Both methods are exposed as actions.

What is Route controller?

Routing controllers allow you to create the controller classes with methods used to handle the requests. Now, we will understand the routing controllers through an example. Step 1: First, we need to create a controller. We already created the controller named as 'PostController' in the previous topic.

How do Rails routes work?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


2 Answers

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 :)

like image 113
Ketan Deshmukh Avatar answered Nov 07 '22 08:11

Ketan Deshmukh


try this in routes.rb

match "/users/another_new " => "users#another_new", :as => 'another_new' 

then you can do

link_to "MyUrl", another_new_path

this should work. Good luck.

like image 45
Jorge Zuverza Avatar answered Nov 07 '22 08:11

Jorge Zuverza