【发布时间】:2011-11-09 00:09:43
【问题描述】:
我正在开发一个非常基本的练习应用程序,用户可以在其中创建多个引用。我遇到的问题是我无法更新我的报价。我有很多东西,并在这里和谷歌上阅读了其他问题,但无法弄清楚我做错了什么。这是我的代码:
#User Model
class User < ActiveRecord::Base
has_many :quotations, :dependent => :destroy
attr_accessible :quotations
accepts_nested_attributes_for :quotations, :allow_destroy => true
end
#Quotations Model
class Quotation < ActiveRecord::Base
attr_accessible :quote_text, :author, :quote_type, :category, :tags, :user_id
belongs_to :user
end
报价控制器
class QuotationsController < ApplicationController
before_filter :get_user
def get_user
@user = User.find(params[:user_id])
end
def edit
@quotation = @user.quotations.find(params[:id])
end
def update
@quotation = @user.quotations.find(params[:id])
if @quotation.update_attributes(params[:id])
redirect_to user_quotation_path :notice => "Successfully updated quotation."
else
render :action => 'edit'
end
end
end
【问题讨论】:
-
已解决 if @quotation.update_attributes(params[:id]) 只是更新 id,这意味着没有任何改变,因为 id 不在编辑表单上。正确的代码将传递 params[:quotation] 来更新 qoutation 参数。 if @quotation.update_attributes(params[:quotation])
标签: ruby-on-rails parameters parameter-passing nested-attributes