Joshua
Joshua

Reputation: 371

Update a model column value by clicking a link 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.

Upvotes: 0

Views: 591

Answers (2)

zer0uno
zer0uno

Reputation: 8030

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

Upvotes: 1

vee
vee

Reputation: 38645

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

Upvotes: 0

Related Questions