Take the 2-minute tour ×

I'm trying to implement a button to update a single attribute (setting an "Active" boolean value to true) for records. After digging around online, I found the following code to 'solve' the problem:

button_to "Add", movie_path(m, active: true), confirm: "Add this movie?", method: :put, class: "btn"

The view creates the button just fine, and the application carries out the command properly, running through my 'update' action in the MoviesController:

    def update
      @movie = Movie.find(params[:id])
      if @movie.update_attributes(params[:movie])
        flash[:notice] = "Movie was successfully updated"
        flash[:error] = "Movie was not changed"
      redirect_to root_path

After repeatedly clicking this button, and restarting my Thinking Sphinx engine several times (thinking the reason the object still showed up on my 'inactive movies' table was simply because the collection needed to be reindexed), I took a look at my console output (I think that's what it's called. It's just the stdout that shoots up all the started GET "/assets..." mumbo-jumbo, and I saw this:

    Started PUT "/movies/334?m%5B%3Aactive%5D=true" for at 2013-01-10 22:41:41 -0600
    Processing by MoviesController#update as HTML
      Parameters: {"authenticity_token"=>"qpcfMEVttjnQJ3Cv2f+tTYBu3/gujijQtn2+17YVPno=", "m"=>{":active"=>"true"}, "id"=>"334"}
      Movie Load (0.4ms)  SELECT `movies`.* FROM `movies` WHERE `movies`.`id` = 334 LIMIT 1
       (0.1ms)  BEGIN
      Movie Exists (0.6ms)  SELECT 1 AS one FROM `movies` WHERE (`movies`.`title` = BINARY '1 Hope' AND `movies`.`id` != 334 AND `movies`.`rec_form` = 'DVD-Rom') LIMIT 1

       (0.2ms)  COMMIT
    Redirected to
    Completed 302 Found in 114ms (ActiveRecord: 1.3ms)

Check out that Movie Exists line. WHERE ... movies.id != 334...even though the line right above it FINDS the record using that id. That seems bad right? Just be looking at the code I've posted here, can anyone see something I'm doing to have it try to update records that aren't matching the associated movie.id? I haven't seen any other records getting broken by this code. If any additional files are needed for inspection, let me know.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

It looks like you have a validation on movie title being unique. That Movies Exists line is probably OK.

The parameters are weird - why is params including an 'm' parameter. Your looking for a 'movies' parameter and the 'm' object should have been a Movie. I'd look into that.

Here's a button_to example...

= button_to 'test', movie_path(m, 'movie[active]' => true), confirm: "Add this movie?", method: :put, class: "btn"

But, I don't think button_to is the best choice here. It just builds a form, you can do that instead with the following which doesn't require a hard coded attribute name...

= form_for [@movie] do |f|
    = f.hidden_field :active, value: true
    = f.submit 'Add', :confirm => 'Add this movie?'
share|improve this answer
That worked great! The reason I'm using a button_to is because this button is appearing in a row in a table; I'm not using a form here. But thanks for the insight! –  Brad Rice Jan 13 '13 at 19:28
add comment

You parameters are



params[:m] = {":active"=>"true"}

So change




and it should work

share|improve this answer
This was very close to answering my question, you just suggested I change my params in the controller, when I only needed to change how I named params in the view, so upvote! Thanks a lot! –  Brad Rice Jan 13 '13 at 19:27
add comment

Your Answer


