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
<%= 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 %>
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.
Related
I have following routing problem in Rails 5:
<%= link_to product.id, product %>
generates a link like this
localhost:3000/products/12345
What I want is a link to the "ext" action in the products controller:
localhost:3000/products/ext/12345
If I try to build a link like this
<%= link_to 'To the product', :controller => :products, :action => :ext %>
it gives back following error:
No route matches {:action=>"ext", :controller=>"products"}
In the routes.rb I have
get "products/ext/:id", to: "products#ext"
Thanks for help!
Modify your routes to
get "products/ext/:id", to: "products#ext", as: :products_ext
and change your view to
<%= link_to products_ext_path(product) %>
I'm following this answer on how to clone a record.
I can't though workout how to phrase the link and route it.
It is in my #miniature show view so I thought it should be something like
<%= link_to 'clone', :controller => :miniatures_controller, :action => :clone %>
and the route
match 'clone', to: 'miniatures#clone', via: 'get'
but this is clearly wrong. I am using #miniature in place of the above answer's #prescription.
What if you just use clone_path:
<%= link_to 'clone', clone_path %>
Cause rake routes shows just clone route. It works with the same routes.
If you are not satisfied with route and you should pass parameters (like miniature_id), add member to your resource (probably nested), like:
resources :miniatures do
member do
get 'clone'
end
end
This will be clone_miniature_path where you should pass #miniature:
<%= link_to 'clone', clone_miniature_path(#miniature) %>
I have a page on the website, which has lots of anchor link eg #menu | #sauces etc
On the page itself the links work fine, its brilliant.
However when I am on a different controller/view, The links do not take me back to the main controller, and to the anchor point clicked.
here is an example of one anchor link, which is in the header (which is on ALL controller views)
<%= link_to '#main', :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
That is in :controller => "main", :action => "index"
When I am in another controller, for example my locations controller,
The links become like this localhost:3000/locations#menu
It should really be localhost:3000/#menu
The root is set to go to the main controller and index action.
Here is my routes.rb file
root :to => "main#index"
match 'admin', :to => 'access#admin_index'
match 'locations', :to => 'ranch_locations#locations'
match ':controller(/:action(/:id))(.:format)'
You need to specify that it is going to a different controller.
<%= link_to '#main', :controller => "main", :action => "index", :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
You should use root_path(:anchor => :main) instead of "#main"
link_to "Comment wall", profile_path(#profile, :anchor => "wall")
# => Comment wall
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
upd:
<%= link_to '#main', :id => 'menu_link' do %>
should be changed to
<%= link_to root_path(:anchor => :main), :id => 'menu_link' do %>
I am using devise for authentication on my project. I created a basic controller called panel with rails. When I am at the home url the link works as it should. But for some reason the link_to in my layout file attempts to find a route that does not exist when I go to /users/sign_in, the default login for devise.
In my layout file I have this link that should always be shown.
<%= link_to "Panel", :controller => "panel", :action => "index" %>
when I attempt to access the default user login path on devise /users/sign_in it gives the error: ActionController::RoutingError in Devise/sessions#new
No route matches {:controller=>"devise/panel"} from the layout file.
routes:
get "panel/index"
get "home/index"
devise_for :users
It looks like the routes go into some sort of devise scope when I click on the link for the users/sign_in path.
Try this:
routes.rb:
get "panel/index" => 'panel#index', :as => 'panel'
In your controller:
<%= link_to "Panel", panel_path %>
get "panel/index", :as => :panel_index
<%= link_to "Panel", panel_index_path %> |
Doing the following also solves it,
<%= link_to "Panel", :controller => "/panel", :action => "index" %>
Source: https://github.com/plataformatec/devise/issues/471
In Rails 3, is there a way to link to a controller's action using ajax without having a named route?
I tried <%= link_to 'Reload', '#', url_for(:action => :reload, :id => #user.id), :remote => true, :method => 'post' %>
but it returns with the error No route matches {:controller=>"users", :id=>2, :action=>"reload"}
My main concern is that I don't want the action to be called by someone typing in a route in the address bar. Is there another way to go about this?
Thanks!
Tim
if your User resource is in your routes.rb file then you need to add a route to the 7 restful actions.
resources :user, :member => {:reload => :get}
That will give you the route to work with
<%= link_to "Reload", user_reload_path(current_user)%>
and that should work for reloading your user object
rake routes
that will show you all your routes you can work with