English 中文(简体)
鲁比代码的一行, 我不明白, 给了我一个大任务错误
原标题:A line of Ruby code, which I don t understand, is giving me a mass-assignment error

目前我正在阅读《行动中的铁路第3号》。这本书创建了一个应用程序,人们可以在其中创建项目,每个项目都可以创建票票。它创造了三个模式:

项目:

    class Project < ActiveRecord::Base
      attr_accessible :name
      validates :name, presence: true
      has_many :tickets, :dependent => :destroy
    end

icket :

    class Ticket < ActiveRecord::Base
      belongs_to :project
      belongs_to :user
      attr_accessible :description, :title
      validates :title, presence: true
      validates :description, presence: true, :length => { :minimum => 10 }
    end

用户和用户 :

    class User < ActiveRecord::Base
      devise :database_authenticatable, :registerable, :confirmable,
             :recoverable, :rememberable, :trackable, :validatable
      attr_accessible :email, :password, :password_confirmation, :remember_me
    end

当我在罚单控制器内部的创建动作中添加以下一行:

    @ticket = @project.tickets.build(params[:ticket].merge!(:user => current_user))

我得到了这个错误 < code> 无法调用质量指定的保护属性 : 用户 。 现在,我无法真正理解要合并什么! 以及为什么: 用户正在被传递或为什么获取错误。 我知道通常我必须包含 Attr_ accessible: 方法的质分配属性。 但这次属性是一个类, 所以我不知道如何处理这个属性 。

help, mike

最佳回答
@ticket = @project.tickets.build(params[:ticket])
@ticket.user = current_user

除非有原因你需要这个 单行线?

@ticket = @project.tickets.build(params[:ticket].merge!(:user_id => current_user.id))
问题回答

将此添加到您的车票模型应该有效。 您必须指定要接受的嵌套属性, 以单一形式使用 。

attr_accessible :description, :title, :user     
accepts_nested_attributes_for :questions

http://appi.rubyonrails.org/class/AspecialRecord/NestedAttriptes/ClassMethods.html>http://api.rubyonrails.org/class/AspecialRecord/NestedAttriptes/ClassMethods.html





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签