Undefined method `service_path' when adding best_in_place

I have an application that has customers and services. Customers has_many services and service belongs_to customer. In the customer show page, I can add or delete services. But I can not edit services, so I’m using best_in_place to do that. but is not working. It returns undefined method `service_path’. I will appreciate any help, I’m new in software.

here is the repository link if you want to check the whole app.

I restarted the server, but it keeps returning the same error.

customer/show.html.erb

   <% @customer.service.each do |service| %>
      <%= best_in_place service, :process  %>
   <% end %>

customer_controller.rb

def show
    @customer = Customer.find params[:id]
    @service = @customer.service.order('created_at DESC').limit(4)
  end


  def edit
    @customer = Customer.find params[:id]
  end

service_controller.rb

  def edit
    @customer = Customer.find params[:id]

    @service = @customer.services.find(params[:id])
  end


  def update
    @service = Service.find(params[:id])

    respond_to do |format|
      if @service.update(service_params)
        format.html { redirect_to @service, notice: 'Service was successfully updated.' }
        format.json { render :show, status: :ok, location: @service }
      else
        format.html { render :edit }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end

routes.rb

resources :customers do
    resources :services

  end

I think you should start by following Rails convention when defining relations. In some places you use customer.service and in other customer.services.
Proper way to do it is to define in your model Customer relation like this: has many :services.
I don’t know ‘best_in_place’ gem, but it looks like the path it’s searching for shouldn’t be nested like yours. I mean:

resources :customers do
  resources :services
end

does not generate service_path but customer_service_path or sth like that (because it’s nested in customers). You can check your routes by runnig rails routes in terminal.

Maybe you can provide explicit path to best_in_place and it would fix your problem without changing your routes.rb file.

Thank you but it’s still not working.

I did that:

<td><%= best_in_place customer_service_path(), :process %></td>

and now it is giving an error:

No route matches {:action=>"show", :controller=>"services", :id=>"2"}, missing required keys: [:customer_id]

Here is my customer controller and my view

You need to pass params to the customer_service_path as you did line below:
customer_service_path(service.customer, service)
This is what error message is saying - because service is nested in customers you need to provide customer id

Once again thank you. It seems like it worked but now it’s giving another error. As you know I’m trying to render the index page of service in the show page of customer.

The error is:

this how I have the service/index in the customer/show

Take a look at the best_in_place readme.
Usage is: best_in_place object, field, OPTIONS, so your object is service, field which you want to edit is process (I think) and in OPTIONS you should provide URL. So it would look sth like that:

best_in_place(service, :process, url: customer_service_path(service.customer, service))

In controller you don’t need to declare @service, because you use @customer.services anyway

1 Like

Thank you very much! It’s working fine now. You saved my life jeje