im currently trying to use the resource but one problem im having , that when i do the following
resource :orders
the route /orders dosent route to OrdersController#index rather it points to the show action of the controller, how can i fix this issue ?
becuase of this problem im having to do this which i feel is kinda hack and not good
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
this is my routes.rb file
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
resources :users do
resource :orders, only: %i[show create index]
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
resource :sessions, only: [] do
post 'login', action: :create
post 'logout', action: :destroy
get 'login', action: :new
end
resources :products
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
thanks for your answer :)
Don't use
resource :orders
use
resources :orders
You would only use resource when the item orders is a single entity in your application... which is to say you're using the plural to refer to that one item.
Move the resource for orders outside of the Users routes.
Just and FYI, you can have both the full resource outside of Users and then those restricted routes inside Users, but I'm not sure what the goal is here so it is up to you to decide that.
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
resources :orders
resources :users do
resource :orders, only: %i[show create index] <-- not sure if this remains here
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
...
Related
I've just added resources: :favorites, nested inside resources: :deals, to my Rails app. All of a sudden, visiting the deals/deal_slug/favorites/new, after it renders, fires off a GET request to deals/favicon.ico which of course activates the show route, which fails to find anything with the slug of favicon.ico.
It appears to ONLY be the favorites/new route that causes this request, and I've tried commenting out all of favoritescontroller#new as well as the new view, with no change.
Commenting out link href="../../favicon.ico" rel="icon" in my layout fixes it of course, but I'd love to keep that in my layout and figure out why the heck this problem JUST started!
My routes file:
Rails.application.routes.draw do
# what are these??
get 'regions/index'
get 'regions/show'
root "deals#index"
resources :deals, param: :slug do
resources :favorites
end
resources :favorites
get 'my-deals', to: 'deals#my_deals', as: :my_deals_path
resources :regions, param: :slug, only: [:index, :show] do
resources :deals, only: [:index, :show], param: :slug
# resources :deals, only: [:index, :show, :edit, :destroy]
end
resource :preferences
ActiveSupport::Inflector.inflections {|inflect| inflect.irregular 'preferences', 'preferences'} # fix route helper paths so that form_for works
get '/preferences/delete_airport/:airport_id', to: 'preferences#destroy', as: 'delete_home_airport'
resources :vacations
get 'pry', to: 'application#pry'
# ------- DEVISE STUFF --------
devise_for :users, controllers: {
omniauth_callbacks: 'users/omniauth_callbacks',
preferences: 'users/preferences',
}
devise_scope :user do
get "/sign_out", to: 'devise/sessions#destroy', as: 'user_sign_out'
get "/sign_in", to: 'users/sessions#new', as: 'user_sign_in'
get "/sign_up", to: 'devise/registrations#new', as: 'user_sign_up'
get "/user/preferences", to: 'users/preferences#index', as: 'user_preferences'
get "/user/preferences/edit", to: 'users/preferences#edit', as: 'edit_user_preferences'
patch "/user/preferences/:id/edit", to: 'users/preferences#update'
end
devise_for :admins, path: 'admin', controllers: { sessions: 'admins/sessions' }
devise_scope :admin do
get "/admin", to: 'admins/sessions#portal', as: 'admin_root'
get "/admin/sign_out", to: 'devise/sessions#destroy', as: 'admin_sign_out'
end
end
FavoritesController:
class FavoritesController < ApplicationController
def new
prefs = current_user.preferences
#favorite = Favorite.new deal: Deal.find_by(slug: params[:deal_slug]), preference_id: prefs.id
end
def create
#favorite = Favorite.create(favorite_params)
redirect_to preferences_path
end
def favorite_params
params.require(:favorite).permit :preference_id, :deal_id, :comment
end
end
The request for the favicon icon is made by the browser, your code does not trigger that request.
The problem is your favicon href, you are making it relative to the current path: "../../favicon.ico" instead of absolute "/favicon.ico". You'll get the same problem if you have other routes with more than 2 levels since the route is relative.
You should add the favicon.icon somewhere on your public folder and point at it with an absolute path instead. If the icon is in /public/favicon.ico then configure the layout's favicon tag with href="/favicon.ico", if you put the icon somewhere else like /public/icons/favicon.ico then change the link tag to href="/icons/favicon.ico".
I my routes file, I have defined a resource
namespace :admin do
resources :invoices, only: [:index, :new]
end
Then I've got a route rule with corresponding path helper new_admin_invoice_path
new_admin_invoice GET /admin/invoices/new(.:format) admin/invoices#new
But how can I add two more new rules, so thуe look like
new_admin_incoming_invoice GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
new_admin_outgoing_invoice GET /admin/invoices/new/outgoing(.:format) admin/invoices#new {:type=>:outgoing}
I tried add them manually
resources :invoices, only: [:index, :new] do
get 'new/incoming', on: :collection, action: :new, type: :incoming
get 'new/outgoing', on: :collection, action: :new, type: :outgoing
end
But got wrong result
new_incoming_admin_invoices GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
new_outgoing_admin_invoices GET /admin/invoices/new/outgoing(.:format) admin/invoices#new {:type=>:outgoing}
How can I get exactly that routes with path helpers what I need?
Here is the easy rails way from official guide
resources :invoices, only: [:index] do
get 'incoming', on: :new, type: :incoming, action: :new
end
Results to
incoming_new_admin_invoice GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
Try by using a scope like this:
scope "/admin" do resources :invoices end
I'm trying to define custom routes to my controller and I need to use some of the default routes too. Is there any simple solution?
So far I've something like this
resources :users do
member do
get 'users/:id', to: 'users#show'
delete 'users/:id', to: 'users#destroy'
end
collection do
post 'users', to: 'users#create'
post 'users/login', to: 'users#login'
end
end
resources :users, :only => [:show, :destroy, :create, :login]
I don't need nor want the index route but with this settings it's still trying to route GET users/ to user_controller index method.
I know that there is probably some simple and obvious answer but I'm not able to find it.
Thank's in advance.
You got your routes wrong. The resources :users generates seven default routes which include the index route as well. You need to tweak the code to below
resources :users, :only => [:show, :destroy, :create] do
collection do
post 'login', to: 'users#login'
end
end
Note:
If you noticed, I've removed the custom routes for show,create and delete as they are generated by default.
Your first line defines the route to the index action. Define a resource once only. Read the routing guide.
resources :users, :except => [:index] do
collection do
post 'users/login', to: 'users#login'
end
end
Run rake routes from the command line in your project root folder to see all your route definitions.
I'm working on an assignment for school and I'm receiving a routing error that I don't understand.. I'm trying to integrate a voting feature to allow users to up vote or down vote on various posts on the site. However, I keep getting the following error:
Routing Error
No route matches [POST] "/topics/post_up_vote_path(post)"
I recently updated my routes.rb file to look like this to implement shallow nesting:
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#up_vote', as: :up_vote
post '/down-vote' => 'votes#down_vote', as: :down_vote
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
Now my routes for up_votes and down_votes look like this:
post_up_vote_path POST /posts/:post_id/up-vote(.:format) votes#up_vote
post_down_vote_path POST /posts/:post_id/down-vote(.:format) votes#down_vote
From the error I receive above it looks like my app is searching for /topics/post_up_vote_path(post) when it should be searching for /posts/:post_id/up-vote . I'm a bit stuck at this point, not sure how to get things to route correctly..
Here's the relevant GitHub branch associated with this project for further reference to other files:
https://github.com/jlquaccia/Bloccit/tree/checkpoint-49-voting
In your voter partial (app/views/votes/_voter.html.erb) you pass the string 'post_up_vote_path(post)' as the url argument for link_to. You just need to get rid of the quotes, so change the link_to to this:
link_to " ", post_up_vote_path(post), class: 'glyphicon glyphicon-chevron-up', method: :post
You will have to do the same with the down vote link.
I have some nested nested resources:
resources :assessments do
member do
get 'result'
end
resources :respondents, only: [:new, :create] do
collection do
post "invite", to: :invite_all
get "invite", to: :new_invite
end
end
end
For the line resources :respondents, only: [:new, :create] is it possible to set the action for the new and crate actions? You can use to: to set the action for a single resource. I'd like to avoid writing match statements if I can and keep things resourceful.
What motivates me to ask this is I'd like to be able to specify the action for a nested resource rather than have it route to the child resource's action. For example:
If I define
resources :assessments do
resources :respondents
end
the path /assessments/:id/respondents/new will route to respondents#new. The problem with this it forces me to add logic to the new action to determine if the route contains the assessment id or not and then render the correct view. I'd like to be able to send the nested resource to a different action. Is there a "rails way" to do this?
Why not just not nest the resources like this:
resources :assessments do
member do
get 'result'
end
end
resources :respondents, only: [:new, :create] do
collection do
post "invite", to: :invite_all
get "invite", to: :new_invite
end
end
Or to keep nesting for other actions (e.g. index) just define the new and create actions separately.
resources :assessments do
member do
get 'result'
end
resources :respondents
end
resources :respondents, only: [:new], path_names: { new: 'make' } do
collection do
post :generate
post "invite", to: :invite_all
get "invite", to: :new_invite
end
end
This will create these routes:
result_assessment GET /assessments/:id/result(.:format) assessments#result
assessment_respondents GET /assessments/:assessment_id/respondents(.:format) respondents#index
POST /assessments/:assessment_id/respondents(.:format) respondents#create
new_assessment_respondent GET /assessments/:assessment_id/respondents/new(.:format) respondents#new
edit_assessment_respondent GET /assessments/:assessment_id/respondents/:id/edit(.:format) respondents#edit
assessment_respondent GET /assessments/:assessment_id/respondents/:id(.:format) respondents#show
PATCH /assessments/:assessment_id/respondents/:id(.:format) respondents#update
PUT /assessments/:assessment_id/respondents/:id(.:format) respondents#update
DELETE /assessments/:assessment_id/respondents/:id(.:format) respondents#destroy
assessments GET /assessments(.:format) assessments#index
POST /assessments(.:format) assessments#create
new_assessment GET /assessments/new(.:format) assessments#new
edit_assessment GET /assessments/:id/edit(.:format) assessments#edit
assessment GET /assessments/:id(.:format) assessments#show
PATCH /assessments/:id(.:format) assessments#update
PUT /assessments/:id(.:format) assessments#update
DELETE /assessments/:id(.:format) assessments#destroy
generate_respondents POST /respondents/generate(.:format) respondents#generate
invite_respondents POST /respondents/invite(.:format) respondents#invite_all
GET /respondents/invite(.:format) respondents#new_invite
new_respondent GET /respondents/make(.:format) respondents#new
This way you get new and create outside of of nested assessments.
I created these routes real quick locally and this is what was generated when running rake routes.