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
Related
I have custom CRUD routes for example - for profiles
get '/profiles', to: 'profiles#index'
get '/profiles/new', to: 'profiles#new', :as => 'new_profile'
post '/profiles', to: 'profiles#create'
get '/profiles/edit/:id', to: 'profiles#edit', :as => 'profile'
patch '/profiles/edit/:id', to: 'profiles#update'
get '/profiles/get_profiles', to: 'profiles#get_profiles'
It works okay. But I do the same routing for profile skills, wich is under relation of profile. Routes of ProfileSkills looks like this
get '/profiles/:profile_id/profile_skills', to: 'profile_skills#index'
get '/profiles/:profile_id/profile_skills/new', to: 'profile_skills#new', :as => 'new_profile_skill'
post '/profiles/:profile_id/profile_skills', to: 'profile_skills#create'
get '/profiles/:profile_id/profile_skills//edit/:id', to: 'profile_skills#edit', :as => 'profile_skills'
patch '/profiles/:profile_id/profile_skills/edit/:id', to: 'profiles#update'
When I under the route for creating the new item
http://localhost:3000/profiles/1/profile_skills/new
It throw an exception
No route matches {:action=>"edit", :controller=>"profile_skills", :profile_id=>"1"}, missing required keys: [:id]
On form for line
<%= form_for #profile_skill do |form| %>
Why he don't understand that I'm under the 'new' route and it looking for 'edit', when i'm under the 'new'?
This problem is only when i'm on sub-routes. In 'Porfile' routes for example, if works fine.
In your routes use this
resources :profiles do
resources :profile_skills
end
this will provide you the routes like this
profiles/:profile_id/profile_skill points to index action of profile_skill
profiles/:profile_id/profile_skill/new points to new action of profile_skill
profiles/:profile_id/profile_skill/:profile_skill_id points to show action of profile_skill
profiles/:profile_id/profile_skill/:profile_skill_id/edit points to edit action of profile_skill
and so on.
for more help visit Rails Routing
The following code isn't working
<p>Tags: <%= #video.tags.map { |t| link_to t.name, tag_path(t)}.join(', ') %></p>
Rails complains about undefined method 'tag_path' for #<#<Class:0x007fd0f0260890>:0x007fd0ebce5f68>
I think tag_path is just not the right url_helper, can anybody explain which one to use here?
My rake routes
video_tags GET /videos/:video_id/tags(.:format) tags#index
POST /videos/:video_id/tags(.:format) tags#create
new_video_tag GET /videos/:video_id/tags/new(.:format) tags#new
edit_video_tag GET /videos/:video_id/tags/:id/edit(.:format) tags#edit
video_tag GET /videos/:video_id/tags/:id(.:format) tags#show
PATCH /videos/:video_id/tags/:id(.:format) tags#update
PUT /videos/:video_id/tags/:id(.:format) tags#update
DELETE /videos/:video_id/tags/:id(.:format) tags#destroy
videos GET /videos(.:format) videos#index
POST /videos(.:format) videos#create
new_video GET /videos/new(.:format) videos#new
edit_video GET /videos/:id/edit(.:format) videos#edit
video GET /videos/:id(.:format) videos#show
PATCH /videos/:id(.:format) videos#update
PUT /videos/:id(.:format) videos#update
DELETE /videos/:id(.:format) videos#destroy
Apart from that, I did not change my routes.rb, which looks like this:
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
root 'welcome#index'
resources :articles do
resources :comments
end
resources :videos do
resources :tags
end
end
Alright, I got it working this way:
Added resource to routes.rb:
resources :videos do
resources :tags
end
With the routes set up, I changed my RHTML to look like this:
<p>
Tags:
<%= raw #video.tags.map {
|t| link_to t.name, video_tag_path(:video_id => #video.id, :id => t.id)
}.join(', ') %>
</p>
Had to specify the video_id and tag id parameter explicitly.
For tags, you have to generate a controller afterwards.
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
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.
In my AdminController, I have methods named as edit, update and update_admin. And in route.rb
resources :session, :only => [update]
match '/:controller(/:action(/:id))'
end
When I navigate the url '/users/edit/1' matches. From this page i want to call the action method in update_admin in AdminController. How to do this?
My edit.erb has
<%= render :partial => '/submit', :locals =
> {:button_html => f.submit('Update'), :validate_present => true}
%>
at first, to check your routes go to console and do
rake routes | grep session
there you will get list all routes of your rails application (matched to the grep)
at second i dont get your routes.rb
would you do
resources :session, :only => [update] do
match '/:controller(/:action(/:id))'
end
or
resources :session, :only => [update]
match '/:controller(/:action(/:id))'
end
these are 2 different things. i think you want the last one. but here we go, there is another problem
resources :session, :only=>[update]
this throws an error.(undefined local variable or method `update' for #)
if you want to specify actions, you need to do it as a key
resources :session, :only=>[:update]
but you also want the edit message, (edit is the form, update the action to save the changes) so you have to do
resources :users, :only=>[:edit, :update]
now check your rake routes and see voila!
edit_session GET /session/:id/edit(.:format) {:action=>"edit", :controller=>"session"}
session PUT /session/:id(.:format) {:action=>"update", :controller=>"session"}
//edit
if you want to do this in your admin_controller you should have a namespace
#i take example for a user
namespace :admin do
resources :user :only => [:edit, :update]
end
if you want now to link to it from a view the route is named
edit_admin_user_path
and in a form you need to bring the namespace also in the form_for like:
=form_for [:admin, #user] do |f|