Reputation: 766
(sorry for my english)
I'm trying my first rails web app. Here is my code:
Model:
class Movie < ActiveRecord::Base
attr_accessible :about, :title, :url
end
Controller:
class MoviesController < ApplicationController
def show
@movie = Movie.find(params[:id])
end
def update
@mov = Movie.find(params[:id])
if @mov.update_attributes(params[:id])
redirect_to movies_path, :notice => "Your movie has been updated."
else
render "show"
end
end
def destroy
end
end
<%= form_for @movie do |m| %> <center> <table> <td valign='middle' align='right'> Title: </td> <td> <%= m.text_field :title %> </td> <tr> <td valign='middle' align='right'> Description: </td> <td> <%= m.text_area :about %> </td> <tr> <td valign='middle' align='right'> URL: </td> <td> <%= m.text_field :url, :id => "url" %><input type='file' onchange="document.getElementById('url').value = this.value" /> </td> <tr> <td> </td> <td> <center> <%= m.submit %> </center> </td> </table> </center> <% end %>
so this is an update
action and this give an error message after press the update form button
error is:
undefined method `stringify_keys' for "15":String
so I try again with Movie.find(params[:id].to_i)
and the output is:
undefined method `stringify_keys' for 15:Fixnum
thanks for answers, have a good day!=)
Upvotes: 0
Views: 1826
Reputation: 1409
if @mov.update_attributes(params[:id])
Its because of the above line. Here you are trying to update the @mov attribute with the params[:id] which is wrong.
the update action in the controller should be as follow.
def update
@mov = Movie.find(params[:id])
if @mov.update_attributes(params[:movie])
redirect_to movies_path, :notice => "Your movie has been updated."
else
render "show"
end
end
Basically the update_attributes will check for hash as a parameter like below.
@mov.update_attributes(:about => "Sample About", :title => "Sample Title", :url => "Sample URL")
Upvotes: 5