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"
else
flash[:error] = "Movie was not changed"
end
redirect_to root_path
end
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 127.0.0.1 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 http://0.0.0.0:3000/
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.
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…
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…