Tag: 嵌套属性

Rails 4 – 嵌套模型(2级)不保存

关于嵌套模型的Noob问题。 我正在使用Rails 4并尝试创建嵌套模型,如下所示: 调查有很多问题每个问题有很多答案 我正在关注Rails Casts第196集以相同的forms创建调查,问题和答案。 Surevey和Realted问题得到保存,但答案不会保存到数据库中。(但是答案字段显示正确。) 我非常感谢您对此的投入。 谢谢,迈克 surveys_controller.rb def index @surveys = Survey.all end def new @survey = Survey.new 3.times do question = @survey.questions.build 1.times { question.answers.build } end end def create @survey = Survey.new(survey_params) respond_to do |format| if @survey.save format.html { redirect_to @survey, notice: ‘Survey was successfully created.’ } format.json { render […]

Rails嵌套属性 – 不创建父级

我有以下型号: class Parent has_many :cars accepts_nested_attributes_for :cars end class Car belongs_to :parent validates :parent, presence: true end 控制器代码: def create parent = Parent.new parent.attributes = parent_params parent.save end def parent_params params.require(:parent).permit(:name, cars_attributes: [:name]) end 当我尝试使用Cars创建Parent时,由于尚未创建Parent因此在Cars上validation失败。 当通过嵌套属性创建validation时,如何使其通过validation?

Rails 5 – 从父控制器上的编辑路径中排除特定实例

我在我的Rails 5应用程序中有用户,提案和潜力的模型。 用户创建提案,然后他们自己和其他人可以创建评论。 模型之间的关联是: 用户 has_many :proposals, dependent: :destroy has_many :potentials 提案 belongs_to :user has_many :potentials, inverse_of: :proposal accepts_nested_attributes_for :potentials, reject_if: :all_blank, allow_destroy: true 潜在 belongs_to :proposal, inverse_of: :potentials belongs_to :user 在我的路线文件中,我有两个潜在的资源。 我不确定我是否已经离开了滑雪道 – 我无法找到一个如何做到这一点的例子。 我有两个: resources :potentials 以及: resources :proposals do resources :potentials 我这样做的原因是,当用户创建提案时,他们可以使用嵌套字段表单来添加潜在属性。 当另一个用户看到该提案时,他们会获得一个新表单来添加:潜在的属性集(他们不会通过提案表单执行此操作)。 在我的潜力视图文件夹中,我有新的渲染forms的视图以及我的提案表单中包含的potential_fields_for(只有提案创建者可以使用嵌套字段)。 新/渲染表单有: f 提案表格包括: 在我的提案控制器中,我正试图找到一种方法来排除第三方创建:来自提案表单中显示的字段的潜力。 def edit @proposal.potentials_build unless […]

如何在新操作中使用与belongs_to关联的嵌套属性?

在update操作上,以下nested_form可以正常工作,但在create我收到此错误: Couldn’t find Student with ID=12 for Cv with ID= 控制器: def new @cv = Cv.new @cv.student = current_student end def create @cv = Cv.new(params[:cv]) if @cv.save redirect_to student_dashboard_path, notice: t(‘activerecord.successful.messages.created’, model: @cv.class.model_name.human) else render ‘new’ end end 模型: class Cv < ActiveRecord::Base attr_accessible :student_attributes belongs_to :student accepts_nested_attributes_for :student end 视图: = f.simple_fields_for :student do […]

Rails 4 – 带有Cocoon gem的嵌套属性

我正在尝试用Rails 4创建一个应用程序。 我正在使用Cocoon gem来创建具有简单forms的嵌套表单。 我有一个资格模型,嵌套在一个配置文件模型中。 协会是: 资格 belongs_to :profile 轮廓 has_many :qualifications accepts_nested_attributes_for :qualifications, reject_if: :all_blank, allow_destroy: true 在我的个人资料表格中,我将资格属性与以下内容相结合: 在我的资格表格属性中,我有: “Your award” %> “Are you currently studying toward this qualification?” %> “When did you graduate?”, collection: (Date.today.year – 50)..(Date.today.year + 5) %> 在我的配置文件控制器中我有: def new @profile = Profile.new @profile.qualifications_build @profile.build_vision @profile.build_personality @profile.addresses_build authorize @profile end […]

Rails避免将嵌套对象的属性写入日志

如何防止rails中嵌套关系的某个参数进入日志文件 – 我正在将LARGE文件写入db中的列,并且不希望rails将其写入日志文件..我知道filter_parameter_logging但是它似乎不适用于嵌套模型 – 我可能只是在错误的位置?

Rails – Cocoon gem – 嵌套表格

我正在尝试用Rails 4创建一个应用程序。 我为表单使用简单的表单gem,并尝试使用Cocoon gem作为表单的嵌套元素。 我问了这个问题,它解释了我想要更详细地实现的内容: Rails – 单个属性的多个条目 我有个人资料模型和资格模型。 协会是: profile.rb has_many :qualifications accepts_nested_attributes_for :qualifications, reject_if: :all_blank, allow_destroy: true qualification.rb belongs_to :profile 配置文件控制器包含强参数中的限定属性: def profile_params params[:profile].permit(:user_id, :title, :hero, :overview, :research_interest, :occupation, :external_profile, :working_languages, :tag_list, qualifications_attributes: [:id, :level, :title, :year_earned, :institution, :_destroy] ) end 我的个人资料表现在有: Your qualifications 我的资格部分表单现在在我的资格视图文件夹中命名为_qualifications_fields.html.erb “Your award” %> “When did you graduate?”, collection: […]

具有嵌套属性的Best_In_Place内联编辑

我目前正在尝试使用best_in_place gem来在HTML表格中进行内联编辑。 我在购物车的展示视图中显示了一个购物车。 在购物车的展示视图中,我可以添加lineItems。 创建LineItem时,还会使用lineItem_id创建新的可用记录,然后在购物车中显示其lineitem。 Cart和LineItem表都来自外部数据库,因此我无法添加列,这就是为什么我不能只为LineItem添加一个可用的布尔属性。 **cart.rb class Cart << AR::Base has many LineItems end **line_item.rb class LineItems <<AR::Base belongs_to Cart has_one :available accepts_nested_attributes_for :available end **available.rb class Available<<AR::Base belongs_to LineItems end **views/cart/show.html.erb @cart.lineitems.each do |line_items| line_items_path, :type => type: :checkbox, collection: %w[No Yes] %> end 我希望能够使用best_in_place编辑html表中的line_item.available.boolean,该表位于购物车展示视图中,但我没有任何运气..任何帮助都会令人惊叹! =]我知道在阅读之后,使用嵌套属性是不可能的,但是如果我能以某种方式摆脱可用模型并在show table中有一个字段,我可以为line_item编辑以查看lineItem是否可用那也很棒。 我对任何想法持开放态度!

用于嵌套索引操作的Rails多态链接

我一直试图找到这个链接几个小时。 我有一个多态关联,其中的集合和分类都有设计。 收集模型 has_many :designs, :as => :targetable 分类模型 has_many :designs, :as => :targetable 设计模型 belongs_to :targetable, :polymorphic => true 为了链接到设计的“show”动作,正确的多态路径将是: link_to polymorphic_path([@targetable, @design]) 但我无法弄清楚如何链接到设计的“索引”页面,以显示与其相应的可定位对象相关的所有设计。 有谁知道到达那里的适当链接?

rails simple_nested_form_for fields_for错误的参数数量

所以我正在使用rails 3.1构建一个表单 collection_url, :html=>{:multipart => true}) do |f| %> “form”, :locals => { :f => f } %> 但部分中的这一行导致了问题: Badges // this following line is raising the error “wrong number of arguments (4 for 3)” ‘certification’, :locals => { :f => certification_form } %> 所以这是模型: class Person :destroy, :order=>:position has_many :certificates, :through=>:certifications accepts_nested_attributes_for :certifications, :allow_destroy […]