Saad
Saad

Reputation: 28586

link_to controller action in view

inside my routes.rb I have resources: x but I also have another controller y, and in one of the pages I want to link to a 'new' action in controller x.

usually If I have a match statement defined in routers like

match 'signin', to: 'session#new'

I can go

<%= link_to "text", signin_path %> 

but what do I do when I use resources as with controller x and need to link to the new action, without having to write match statements out in routes.rb

Thanks

Upvotes: 0

Views: 178

Answers (2)

Dipak Panchal
Dipak Panchal

Reputation: 6036

Try this

  match '/signin' => 'session#new', :as => :new_session

now, your signin path is new_session_path

write in your view file

<%= link_to "SignIn", new_session_path %>

write in your terminal - $ rake routes

you can view your resources and other routes. for that choose your '/signin' path and write that path in your link.

Upvotes: 0

zolter
zolter

Reputation: 7160

<%= link_to 'New', new_controller_name_path %>

And you can view all your routes by type rake routes in your app directory.

And if you want change default path you can write in config/routes.rb smth like this:

match 'controller_name/new', :to => 'controller_name#new', :as => 'only_my_new', :via => :get

And then create link:

 <%= link_to 'New', only_my_new_path %>

Upvotes: 0

Related Questions