Update a model column value by clicking a link ruby on rails - ruby-on-rails

I have a servicebooking model and I am trying update its accept_booking column with a value of 1 when the following link is clicked in my servicebooking show view:
<%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>
I get the following error:
undefined method `acceptbooking_servicebooking_path' for #<#<Class:0x5a35e98>:0x5a2bbf0>
Below I have declared the route in routes.rb
get 'acceptbooking', to: 'servicebookings#acceptbooking'
I have the following acceptbooking method in my servicebookings controller:
def acceptbooking
render nothing: true
#servicebooking = Servicebooking.find(params[:id])
#servicebooking.update_attribute(:accept_booking, 1)
end
my Routes.rb
Appa::Application.routes.draw do
devise_for :users
devise_for :views
get "welcome/index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
get 'myevents', to: 'events#myevents'
get 'myvenues', to: 'venues#myvenues'
get 'myservices', to: 'services#myservices'
get 'make_available', to: 'services#make_available'
get 'myservicebookings', to: 'servicebookings#myservicebookings'
get 'acceptbooking', to: 'services#acceptbooking'
#match 'acceptbooking', to: 'servicebookings#acceptbooking', via: [:get, :post]
resources :events do
resources :comments
end
resources :users do
resources :events
end
resources :venues do
resources :comments do
collection do
post :venuec
end
end
end
resources :services do
resources :comments do
collection do
post :servicec
end
end
end
resources :servicebookings do
resources :services
end
end
Can anyone see where Iam going wrong here? Thanks guys.

Try this: get 'acceptbooking/:id', to: 'servicebookings#acceptbooking', as: 'acceptbooking_servicebooking'

With just the route definition (from your route file):
get 'acceptbooking', to: 'servicebookings#acceptbooking'
the name of the route will be acceptbooking so you should be using acceptbooking_path. But based on your <%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>, it appears like you want a url like /servicebookings/:service_booking_id/acceptbooking. If this is the case then you should update your route definition to:
resources :servicebookings do
resources :services
# Move the following line here from the top.
get 'acceptbooking', to: 'servicebookings#acceptbooking'
end
And this will give you servicebooking_acceptbooking_path (Note the order of servicebooking and acceptbooking here).

Related

Ruby on Rails: rosource RESOURCE_NAME dosent generate index route/action

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
...

RoutingError uninitialized constant Subscribers

Somehow i get this routing error. I don't see the solution
My controller:
class SubscribersController < ApplicationController
def signup
blabla...
end
etc.....
end
My routes.rb
Rails.application.routes.draw do
get 'signup' => 'subscribers/#signup'
resources :events
resources :subscribers
root to: 'events#index'
end
so if i go to http://0.0.0.0:3000/signup
I get the error: RoutingError uninitialized constant Subscribers
/ stands for namespace or folder and # is used for action in the routes. you just need to remove / as signup is an action under SubscribersController:
get 'signup' => 'subscribers#signup'
Since your have resources :subscribers so best way to declare route for non CRUD operation like
resources :subscribers do
member { post :foo } # if is member (operation on single obj) with http post
collection { get :bar } # for collection with http get
end
So the best solution for your problem is
Rails.application.routes.draw do
resources :events
resources :subscribers do
collection { get :signup }
end
root to: 'events#index'
end
For more information see http://guides.rubyonrails.org/routing.html

Placing User ID's within Routes with Devise

I currently have a default devise set up and wanting to put the user id's within my logged in routes and leave the others alone such as Welcome,FAQ, About us etc.
Such as
www.example.com/user-id/offers
My Current Routes File
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :offers do
member do
put :tourcomplete
end
end
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
patch '/balance', to: 'balance#paypal', as: 'balance_paypal'
patch '/withdraw', to: 'balance#withdraw', as: 'balance_withdraw'
I can't find any docs on this and my previous answer to a similar question was very vague and not helpful.
Thanks
You will need to use a nested routes:
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
so now your routes will be /users/:id/offers, run rake routes to check your routes.
if you want to exclude users then you will have to specify the route yourself:
match ':user_id/offers' => 'offers#index'

How to create clean URL in rails nested resource?

I have two controller Stores and Stocks and routes for these two controller is given below:
resources :stores do
resources :stocks,param: :product_id,:only=>[:index] do
get '/:product_id', to: 'stocks#index'
end
end
After rake routes I'm getting the path like:
GET /stores/:store_id/stocks/:stock_id/:product_id(.:format)
But I want to remove :stock_id from that path so that the resultant path will be:
GET /stores/:store_id/stocks/:product_id(.:format)
If it's possible then please help.
Try this:
resources :stores do
resources :stocks, except: :index do
get '/:product_id', to: 'stocks#index', on: :collection
end
end
This will give you:
GET /stores/:store_id/stocks/:product_id(.:format) stocks#index
if you want default index action also then:
resources :stores do
resources :stocks do
get '/:product_id', to: 'stocks#index', on: :collection
end
end
This will give you both index:
GET /stores/:store_id/stocks/:product_id(.:format) stocks#index
store_stocks GET /stores/:store_id/stocks(.:format) stocks#index

Setting up routes in rails

When I put the following code in my routes config :
resources :users do
end
I get all the CRUD operations routes. i.e
/users/new
/users/:id/edit
and so on.
How do I configure routes so I get route like this :
/users/lookup/:search_query
And when users reaches this routes he/she should be taked to lookup method of my controller
I would do :
resources :users do
get :lookup, on: :collection
end
And I would pass the search_query as a parameter. With that you will be more flexible.
resources :users do
get '/lookup/:search_query' => 'users#lookup', on: :collection
end
resources :users do
end
match '/users/lookup/:search_query' => "users#lookup", :as => :user_lookup

Resources