Naarsh
Naarsh

Reputation: 39

Rails update model without edit

I am trying to update something from my view. The fact is for this model, the only field updatable is a boolean that goes from true to false and vice-versa.

That's why I don't want to go through a form in a edit view/controller method.

Here's my button :

 link_to 'Disable', myModel_path(id: @something.id), method: :put
 # I checked, the id is the right one

And here's the controller method I want to go through

# in myModel
def update
   m = myModel.find(params[:id])

   m.update(booleanField: !m.booleanField)

   render nothing: true
end

Also in route I did something like this :

resources :myModels   # with plural

Right now I am having an error that says bad route... I think the problem comes from the path of my link_to in my view.

No route matches [PUT] "/myModels"

What I'm trying to do is to go through the UPDATE controller method and stay in the same page (that i'll reload with some JS with onclick) so I can update my model without giving some troubles to my users.

Any help ? :)

EDIT : Here's the rake routes :

           myModels GET    /myModels(.:format)                                     myModels#index
                    POST   /myModels(.:format)                                     myModels#create
        new_myModel GET    /myModels/new(.:format)                                 myModels#new
       edit_myModel GET    /myModels/:id/edit(.:format)                            myModels#edit
            myModel GET    /myModels/:id(.:format)                                 myModels#show
                    PATCH  /myModels/:id(.:format)                                 myModels#update
                    PUT    /myModels/:id(.:format)                                 myModels#update
                    DELETE /myModels/:id(.:format)                                 myModels#destroy

(I checked the plurality twice ;) )

Upvotes: 0

Views: 779

Answers (1)

Naarsh
Naarsh

Reputation: 39

If anyone is having the same troubles, as per Narasimha's comment here's what you need to put in the view helper link_to :

link_to 'Disable', myModel_url(@something.id), method: :put

Upvotes: 1

Related Questions