I have this in my routes.rb:
resources :profiles, :only => :show
I'm building a link like this: <%= link_to( "profile", profile_path(13)) %> and it's giving me a url of /profile?id=13 - I want it to give me /profiles/13 (as that one works).
What am I doing wrong?
Rake routes result for this resource:
GET /profiles/:id(.:format) profiles#show
Related
I am a bit confused, I have admin section in my project. These are the routes.
Rails.application.routes.draw do
devise_for :users
resources :users
namespace :admin do
get '/' => 'dashboard#index'
resources :dashboard, only: [:index]
resources :categories do
collection do
get :set_active
end
resources :sections, only: [:edit, :new, :create, :update]
end
end
And I have a form.
If I start it like this.
<%= form_for #category, :html => { :multipart => true } do |f| %>
It fails (because it tries to search bad url without admin).
undefined method category_path' for #ActionView::Base:0x00000000019320`
So I defined url.
<%= form_for #category, url: admin_categories_path do |f| %>
It works great when i create new category, but if I want to edit the category with the same form partial, it fails:
No route matches [PATCH] "/admin/categories"
but routes exists
admin_categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_admin_category GET /admin/categories/new(.:format) admin/categories#new
edit_admin_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
admin_category GET /admin/categories/:id(.:format) admin/categories#show
PATCH /admin/categories/:id(.:format) admin/categories#update
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
and I have methods in my model for index, new, create, update, edit, show.
I have no idea how to set up it properly.
One small detail that is missed #category to your route path, so it will build url
/admin/categories/1 where 1 is #category.id Rails builds url from model
<%= form_for([:admin, #category]) do |f| %>
So rails generated route for you /admin/categories/:id but you are trying to access /admin/categories/ with no :id
further reading
I have recently move my project away from the somewhat bloat devise to clearance, though I am experiencing troubles when attempting to log out
I am currently get the error of the route not existing
No route matches [GET] "/sign_out"
routes
resources :passwords, controller: "clearance/passwords", only: [:create, :new]
resource :session, controller: "clearance/sessions", only: [:create]
resources :users, controller: "clearance/users", only: [:create] do
resource :password,
controller: "clearance/passwords",
only: [:create, :edit, :update]
end
get "/sign_in" => "clearance/sessions#new", as: "sign_in"
delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
get "/sign_up" => "clearance/users#new", as: "sign_up"
constraints Clearance::Constraints::SignedIn.new do
root :to => 'shopping/merchants#index', as: :signed_in_root
end
constraints Clearance::Constraints::SignedOut.new do
root to: 'clearance/sessions#new'
end
view
= link_to sign_out_path, method: :delete, class: 'mdl-navigation__link' do
i.material-icons> exit_to_app
= t('.log_out')
The message is telling you there is not get route for sign_out, which is correct. You must do a delete. This means, despite your efforts with method: :delete, the link is executing a get request. There's something about your link_to that is not correct. It likely has to do with passing method while using the block form of link_to.
Try:
<%= button_to "Sign Out", sign_out_path, method: :delete %>
If that works, try:
<%= link_to "Sign Out", sign_out_path, method: :delete %>
If both of those work, then the issue is indeed with the way you're using the block form of the link_to helper and has nothing to do with Clearance.
I thought I'd follow up on this with a little more insights for anyone who found themselves here still wondering exactly what is behind this.
As suggested by Derek, to get this sorted, you'll need to use the button_to instead of link_to, as follows:
<%= button_to "Sign Out", sign_out_path, method: :delete %>
Why can't I just use link_to?
So more insights into this are delivered by this previous SO. Essentially, you can't make a link operate as a DELETE method, only GET.
If you have a look at how link_to is actually rendered in HTML on the page (after Ruby works its magic), you'll see the following:
<a rel="nofollow" data-method="delete" href="/sign_out">Sign out</a>
And it's clear that data-method="delete" is still not going to cut it, and still runs the request as GET.
But I don't want a button, I want a link?
Your best bet is to look at some CSS on the element to get it back to looking like a link, otherwise, you'll need to go the Javascript route.
Non-RESTFUL Clearance Centric Dirty Hack - Not Advised!
Using link_to, change your routes.rb where the clearance route currently says:
delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
to:
get "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
It'll route the GET request to the destroy action on the Clerance controller. I dare say this is not advised and Derek could support why this was not supported in the first place (Devise gem does support this dirty hack)
I've created a controller Pages and some actions for simple pages (contact us, for instance), then I went to routes.rb and created a route to allow users to go directly to /contactus, instead of /pages/contactus.
How can I point link_to to the action, but still getting the right route url?
get :contact_us, to: 'pages#contact_us'
or
get :contact_us, controller: :pages, action: :contact_us
this will generate path contact_us_path or url contact_us_url
HEARE MORE ABOUT ROUTES IN RAILS
#config/routes.rb
resources :pages, path: "", only: [] do #-> has to be above everything in routes file
collection do
get :contact_us #-> url.com/contact_us
get :about #-> url.com/about
end
end
root ...
You'd link to it as follows:
<%= link_to "Contact", pages_contact_us_path %>
You can do this:
get '/contactus', to: 'pages#contactus'
Your link can be:
<%= link_to "Contact Us", contactus_path %>
For more information, see: http://guides.rubyonrails.org/routing.html#connecting-urls-to-code
This is the syntax for a simple route using the contactus action in the pages controller:
get '/contactus' => 'pages#contactus'
or if you want a simpler name for your path:
get '/contactus' => 'pages#contactus', as: :contact
I've created custom routes to route to News model
resources :news, only: [:index] do
collection do
get 'page/:page', action: :index
end
end
get "news/:id(/p/:p)", to: 'news#show', as: 'news'
generate url like this
http://localhost:3000/news/4/page/2 index.html.erb is right
and show.html.erb
<%= link_to 'news', news_path(#news, '2')%>
I hope generate url http://localhost:3000/news/4/p/2
but generate http://localhost:3000/news/4?p=2
I think you've got a route conflict, change this:
get "news/:id(/p/:p)", to: 'news#show', as: 'news'
to this:
get "news/:id(/p/:p)", to: 'news#show', as: 'news_page'
and the use this link:
<%= link_to 'News', news_page_path(#news, '2')%>
I'm trying to post to my registration controller using a link_to link.
I have <%= link_to "Register for Period", registration_path(period_id: period.id), :method => :post %>
Which generates a link like: http://localhost:3000/registrations/6?period_id=25 where the 6 is the event_id. I need to save the period_id and the user_id to the registration database.
I get the following error in the browser: No route matches [POST] "/registrations/6"
What am I doing wrong?
routes:
Mavens::Application.routes.draw do
devise_for :users
resources :events
resources :periods
resources :products
resources :cart_rows
resources :product_requests
resources :inqueries
resources :registrations
match '/profile', to: 'static_pages#profile'
root :to => 'static_pages#home'
get "static_pages/home"
get "static_pages/about"
end
If you put in your routes.rb:
resources :registrations do
member do
post :save_period
end
end
And in your link:
<%= link_to "Register for period",
save_period_registration_path(id: #registration.id, period_id: period.id), :method => :post %>
You will have a route that matches your resquest.
When you only have a resources :registrations rule on your routes.rb, only the default restful routes are created, and there is no POST to a single resource created by default.
I believe you will have to read something about the CSRF token, because if you have a protect_from_forgery on your application_controller, probably this POST request from a single link would not work.
Your routes.rb is missing the required path. It IS a standard path, so adding 'resources :registrations' would work.
If more complex, post your routes.rb and we can tell what to add.