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.
Related
How can I also show a blog post without /posts/ in the URL?
http://www.anthonygalli.com/posts/i-walk-the-line
http://www.anthonygalli.com/posts/50-shades-of-galli
http://www.anthonygalli.com/posts/tips-for-regaining-momentum
Shortened Version (Goal of Question):
http://www.anthonygalli.com/i-walk-the-line
http://www.anthonygalli.com/50-shades-of-galli
http://www.anthonygalli.com/tips-for-regaining-momentum
I have post as a MVC.
routes.rb
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
Change your routes.rb to:
resources :posts, path: '/' do
collection do
get 'about'
get 'feed'
get 'subscribe'
end
end
For documentation on Rails routing check: http://guides.rubyonrails.org/routing.html
Your routes should be updated to look something like this:
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
get ':id' => 'posts#show', :as => :post_by_slug
That as option will be important for linking to your new route if you want to link to the /post/-less paths:
<%= link_to post.title, post_by_slug_path(post) %>
P. S. I believe that the new route will need to be listed last in your routes so that the routes before it can have precedence. (Otherwise, trying to visit /about would try to load a post with a slug named about instead of the posts#about action. You should try this and see if that's true.)
Resource generates the standard CRUD urls, so you have to specify the posts route specifically. Something like this
get '/desired-url' => 'posts#index'
This may not make sense but I'm trying to learn harder stuff and progress, it seems like I'm missing the ID for address but can't seem to find a solution.
I included the url in form_for because when I remove it, the app breaks. But seems like I predefined the url than edit breaks.
<%= form_for([#address.user, #address], :url => user_addresses_path) do |f| %>
Error Readout:
No route matches [PATCH] "/users/1/addresses"
When I remove :url=>
undefined method `user_client_address_path'
routes.rb
Rails.application.routes.draw do
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
rake routes partial output (let me know if more is needed)
user_addresses GET /users/:user_id/addresses(.:format) client/addresses#index
POST /users/:user_id/addresses(.:format) client/addresses#create
new_user_address GET /users/:user_id/addresses/new(.:format) client/addresses#new
edit_user_address GET /users/:user_id/addresses/:id/edit(.:format) client/addresses#edit
user_address GET /users/:user_id/addresses/:id(.:format) client/addresses#show
PATCH /users/:user_id/addresses/:id(.:format) client/addresses#update
PUT /users/:user_id/addresses/:id(.:format) client/addresses#update
DELETE /users/:user_id/addresses/:id(.:format) client/addresses#destroy
If you are using AJAX to submit the form, try adding method: 'POST' to it.
If you are submitting the form normally, try adding method: :post to the form_for hash.
It should end up something like:
<%= form_for([#address.user, #address], url: user_addresses_path, method: :post) do |f| %>
i try to fill twice id in url, but when i send params twice id just one id fill the url id.
My route :
namespace :admin do
resources :stores
get "/:id/new_items"=> 'stores#new_items', as: :store_new_items
post "/:id/create_items"=> 'stores#create_items', as: :store_create_items
get "/:id/show_items/:id"=> 'stores#show_items', as: :store_show_items
get "/:id/items/:id/new_items_sub" => 'stores#new_items_sub', as: :store_new_items_sub
post "/:id/items/:id/create_items_sub" => 'stores#create_items_sub', as: :store_create_items_sub
get "/:id/items/:id/show_items_sub/:id" => 'stores#show_items_sub', as: :store_show_items_sub
end
my view :
<%= link_to "add new items", admin_store_new_items_sub_path(#store.id, #items.id), :class=> "btn" %>
i hope my url like this :
http://localhost:3000/admin/#{store.id}/items/#{items.id}/new_items_sub
but i get same id like this :
http://localhost:3000/admin/#{store.id}/items/#{store.id}/new_items_sub
please tell me when i'm wrong? thanks
you have to create neseted routes for that .have a look at
http://guides.rubyonrails.org/routing.html#nested-resources
for example
resources :publishers do
resources :magazines do
resources :photos
end
end
will accept routes /publishers/1/magazines/2/photos/3
Your params should be unique, so you can't pass more than one different :id params. Instead. you can do something like:
get '/:store_id/show_items/:id', as: :store_show_items
and in view:
<%= link_to 'show items', store_show_items_path(#store.id, #item.id) %>
Also, you should read more about Resources and Nested Resources in Rails, there's probably no need to complicate your life by creating each route independently.
You could refactor this to use nested routes like this (you may have to change controller method names):
namespace :admin do
resources :stores do
resources :items, :only => [:new, :create, :show] do
resources :subs, :only => [:new, :create, :show]
end
end
end
This would give you a few url helpers like this: new_store_item_sub_path(#store.id, #item.id) for the new action and store_item_sub_path(#store.id, #item.id, #sub.id) for the show action.
Run rake routes to see what helpers and routes you have access to.
Have a look here to find out more about nested routes.
Your code can be DRYed up significantly. Hopefully this works; might need some tweaking:
namespace :admin do
resources :stores do
member do
get :new_items, as: :store_new_items
post :create_items, as: :store_create_items
end
get "show_items/:id"=> 'stores#show_items', as: :store_show_items
resources :items do
get :new_items_stub => 'stores#new_items_sub', as: :store_new_items_sub
post :create_items_stub => 'stores#create_items_sub', as: :store_create_items_sub
get "show_items_sub/:id" => 'stores#show_items_sub', as: :store_show_items_sub
end
end
end
Uses Member Routes (see 2.10) & Nested Resources
Nested Resources
The crux of your issue is that you're trying to pass the :id param twice
Fortunately, Rails has a solution to this, in the form of Nested Resources. These work by taking the "parent" id and prepending a singular prefix, such as :store_id, allowing you to use the :id param for another set of methods
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')%>
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|