使用“update_attributes”更新数据库通过“has_many”
我在让我的第一个应用程序(我是一个新手)保存新的关联记录时遇到问题。我有两个具有 has_many/belongs_to 关联的模型(用户和图片)。我已经设置了 userController,以便它可以创建一张新图片,如下所示:
def new_picture
@user = User.find(current_user.id)
@picture = @user.pictures.build
end
def create_picture
@user = User.find(params[:id])
@picture = @user.pictures.build(params[:picture])
if @picture.save
flash[:notice] = "Your picture was successfully added."
redirect_to :action => 'show', :id => @user.id
else
render :template => "new_picture"
end
end
我用它
<%= link_to("add picture", :action => 'new_picture', :id => @user.id) if current_user %>
来添加一张新图片。但我也希望能够进行编辑。所以我用一些新代码更新了用户控制器:
def edit_picture
@user = User.find(current_user.id)
@picture = @user.pictures.find(params[:id])
end
# When the user clicks the save button update record
def update_picture
@user = User.find(current_user.id)
@picture = @user.pictures.find(params[:picture])
respond_to do |format|
if @picture.update_attributes(params[:picture])
flash[:notice] = "Your picture was successfully updated."
redirect_to :action => 'show', :id => @user.id
else
render :template => "new_picture"
end
end
end
并将编辑链接添加到 show.erb:
<%= link_to("edit picture", :action => 'edit_picture', :id => picture.id) if current_user %>
它加载编辑表单很好,数据都在正确的位置,但在保存时它所做的一切给了我错误“ArgumentError in” UsersController#update_picture' 带有我的图片表中的一堆未知密钥。
有人可以解释为什么吗?我觉得这里有一块拼图我还没有完全理解......
提前致谢!
更新:查看代码如下:
<h1>New picture for <%= @user.name %></h1>
<% form_for :picture, @picture, :html => { :multipart => true }, :url => {:action => 'update_picture', :id => @user.id} do |f| %>
I'm having a problem getting my first app (I'm a total newbie) to save a new associated record. I have two models (users and pictures) with a has_many/belongs_to association. I have set up the userController so that it can create a new picture as below:
def new_picture
@user = User.find(current_user.id)
@picture = @user.pictures.build
end
def create_picture
@user = User.find(params[:id])
@picture = @user.pictures.build(params[:picture])
if @picture.save
flash[:notice] = "Your picture was successfully added."
redirect_to :action => 'show', :id => @user.id
else
render :template => "new_picture"
end
end
and I use
<%= link_to("add picture", :action => 'new_picture', :id => @user.id) if current_user %>
to add a new one. But I'd also like to be able to edit. So I updated the usercontroller with some new code:
def edit_picture
@user = User.find(current_user.id)
@picture = @user.pictures.find(params[:id])
end
# When the user clicks the save button update record
def update_picture
@user = User.find(current_user.id)
@picture = @user.pictures.find(params[:picture])
respond_to do |format|
if @picture.update_attributes(params[:picture])
flash[:notice] = "Your picture was successfully updated."
redirect_to :action => 'show', :id => @user.id
else
render :template => "new_picture"
end
end
end
and added the edit link to show.erb:
<%= link_to("edit picture", :action => 'edit_picture', :id => picture.id) if current_user %>
It loads the edit form fine, with the data all in the right place, but on save all it's doing is giving me the error 'ArgumentError in UsersController#update_picture' with a bunch of Unknown key(s) from my pictures table.
Could somebody explain why? I feel like there is one piece of the jigsaw I haven't quite understood here....
Thanks in advance!
UPDATE: View code is as follows:
<h1>New picture for <%= @user.name %></h1>
<% form_for :picture, @picture, :html => { :multipart => true }, :url => {:action => 'update_picture', :id => @user.id} do |f| %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎在视图代码中看不到您的问题,但是您可以作为嵌套路由更优雅地(RESTful)做同样的事情。这样你也许能够更清楚地看到问题。
config/routes.rb:
app/controllers/pictures_controller.rb:
app/views/pictures/edit.html.erb:
并链接到您的编辑表单:
Can't seem to see your problem in the view code, however you can do the same thing more elegantly (RESTful) as a nested route. That way you might be able to see the problem more clearly.
config/routes.rb:
app/controllers/pictures_controller.rb:
app/views/pictures/edit.html.erb:
and to link to your edit form:
我建议将 'accepts_nested_attributes_for :pictures 添加到用户模型中,然后
在视图中执行。
另一种选择是为图片创建一个新的控制器。这可能更简单。
I suggest adding 'accepts_nested_attributes_for :pictures to the user model, and then do
in the view.
Another option is to create a new controller for the pictures. That may be simpler.