使用“update_attributes”更新数据库通过“has_many”

发布于 2024-12-05 00:24:24 字数 1933 浏览 0 评论 0原文

我在让我的第一个应用程序(我是一个新手)保存新的关联记录时遇到问题。我有两个具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很坚强 2024-12-12 00:24:24

似乎在视图代码中看不到您的问题,但是您可以作为嵌套路由更优雅地(RESTful)做同样的事情。这样你也许能够更清楚地看到问题。

config/routes.rb:

resources :users do
  member do
    resources :pictures
  end
end

app/controllers/pictures_controller.rb:

class PicturesController < ApplicationController
  before_filter :find_picture, :only => [:edit, :update]

  def edit
  end

  def update
    if @picture.update_attributes params[:picture]
      flash[:notice] = "Your picture was successfully updated."
      redirect_to user_path(current_user)
    else
      render :edit
    end
  end

  protected
  def find_picture
    @picture = current_user.pictures.find params[:id]
  end
end

app/views/pictures/edit.html.erb:

<%= form_for [current_user, @picture] do |f| %>
<!-- some stuff -->
<% end %>

并链接到您的编辑表单:

<%= link_to_if current_user, 'edit picture',
                edit_user_picture_path(:user => current_user, :id => picture) %>

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:

resources :users do
  member do
    resources :pictures
  end
end

app/controllers/pictures_controller.rb:

class PicturesController < ApplicationController
  before_filter :find_picture, :only => [:edit, :update]

  def edit
  end

  def update
    if @picture.update_attributes params[:picture]
      flash[:notice] = "Your picture was successfully updated."
      redirect_to user_path(current_user)
    else
      render :edit
    end
  end

  protected
  def find_picture
    @picture = current_user.pictures.find params[:id]
  end
end

app/views/pictures/edit.html.erb:

<%= form_for [current_user, @picture] do |f| %>
<!-- some stuff -->
<% end %>

and to link to your edit form:

<%= link_to_if current_user, 'edit picture',
                edit_user_picture_path(:user => current_user, :id => picture) %>
愛上了 2024-12-12 00:24:24

我建议将 'accepts_nested_attributes_for :pictures 添加到用户模型中,然后

<%= form_for @user do |form| %>
  .. user fields

  <%= form.fields_for :pictures do |picture_form| %> 

    .. picture fields

  <% end %>

  <%= form.submit %>
<% end %>

在视图中执行。

另一种选择是为图片创建一个新的控制器。这可能更简单。

I suggest adding 'accepts_nested_attributes_for :pictures to the user model, and then do

<%= form_for @user do |form| %>
  .. user fields

  <%= form.fields_for :pictures do |picture_form| %> 

    .. picture fields

  <% end %>

  <%= form.submit %>
<% end %>

in the view.

Another option is to create a new controller for the pictures. That may be simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文