Rails - toggle boolean on click of a link

120 Views Asked by At

I'm trying to make an app with rails 4 and simple form.

I have models separately called Project, Scope and Finalise. Finalise belongs to Scope. Scope belongs to Project. Project accepts nested attributes for Scope and Finalise. Scope accepts nested attributes for Finalise.

I have an attribute in my Finalise table called :draft.

If a user creates a project and sets :draft as true, then I display a link, which when clicked, will change :draft to false.

I have the following link in my view:

  <%= link_to 'Finalise draft', finalise_toggle_draft_path(@project.scope.finalise.id), method: :patch %>

I have the following method in my controller:

  def toggle_draft
    @finalise = Finalise.find(params[:finalise_id])
    @finalise.draft = false
    @finalise.finalised = Time.now
    @finalise.save
    redirect_to project_path(Project.find(params[:project_id]))
  end

When I try this, I get an error that says: Couldn't find Project without an ID

Does anyone know what I've done wrong?

My routes for finalise are:

finalise_toggle_draft PATCH    /finalises/:finalise_id/toggle-draft(.:format)                                                  finalises#toggle_draft
                            finalises GET      /finalises(.:format)                                                                            finalises#index
                                      POST     /finalises(.:format)                                                                            finalises#create
                         new_finalise GET      /finalises/new(.:format)                                                                        finalises#new
                        edit_finalise GET      /finalises/:id/edit(.:format)                                                                   finalises#edit
                             finalise GET      /finalises/:id(.:format)                                                                        finalises#show
                                      PATCH    /finalises/:id(.:format)                                                                        finalises#update
                                      PUT      /finalises/:id(.:format)                                                                        finalises#update
                                      DELETE   /finalises/:id(.:format)                                                                        finalises#destroy
0

There are 0 best solutions below