Tag: 嵌套属性

无法使用Devise批量分配受保护的属性以创建has_many嵌套模型

我已经看过RailsCast,另一个嵌套属性video,很多SOpost,并与此争吵了一段时间,但我仍然无法弄明白。 我希望它有点小。 我有两个模型, User (由Devise创建)和Locker (又名产品愿望清单),我正在尝试为User注册时创建一个Locker 。 我的登录表单有一个新的Locker名称字段(恰当地称为:name ),我试图分配给在新用户注册时创建的锁定器。 我所遇到的只有: WARNING: Can’t mass-assign protected attributes: locker 我已经在我的两个模型中尝试了accepts_nested_attributes和attr_accesible每个组合,但仍然attr_accesible 。 我可以从日志中看到它正在由Devise #create方法处理,我知道Devise不够聪明,无法创建我想要的模型:) 这是我的两个模型的相关部分: # user.rb class User :lockers # Model nesting access accepts_nested_attributes_for :lockers end 和 # locker.rb class Locker :lockups attr_accessible :name, :description end # lockers_controller.rb (create) @locker = current_user.lockers.build(params[:locker]) @locker.save 我假设我需要覆盖Devise的create方法以某种方式让它工作,但我对rails非常新,并且我已经习惯了黑盒子“神奇”的本质。 如果有人能帮助我,我会非常感激。 已经花了太多时间在这上面,因为它是:) 编辑:我意识到我在我的问题中省略了一些东西。 我的Locker模型有三个属性 […]

params.require()。permit不能按预期工作

我有这个控制器 class PeopleController < ApplicationController def new @person = Person.new @person.phones.new end # this is the action that gets called by the form def create render text: person_params.inspect # @person = Person.new(person_params) # @person.save # redirect_to people_path end def index @person = Person.all end private def person_params params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ]) end end […]

Rails – 我无法使用嵌套表单在我的用户表单上显示位置

我无法使用嵌套表单显示belogns给用户的位置。 我想创建用户并在位置模型中保存地址,城市,州等,并在用户模型中保存名称,用户名和电子邮件。 我在控制台上没有错误。 但是字段(选择或下拉列表,text_fields等)没有显示出来。 数据库有记录。 这是我的user.rb模型: class User < ActiveRecord::Base has_many :locations accepts_nested_attributes_for :locations end 这是我的location.rb模型: class Location < ActiveRecord::Base belongs_to :user end 这是我的用户表单: prohibited this user from being saved:

Rails 4,使用Model.where(related_model_id:1)检索了许多记录,如何在单个视图中显示每个记录?

好的,所以我正在进行测验,作为我的应用程序的一部分。 我有三个嵌套模型测验,问题,答案。 class Quiz lambda { |a| a[:content].blank? }, allow_destroy: true end class Question lambda { |a| a[:content].blank? }, allow_destroy: true end class Answer < ActiveRecord::Base belongs_to :question end 在控制器动作测验中#show我可以检索特定测验的所有问题 def show @quiz = Quiz.find(params[:id]) @question = Question.where(quiz_id: @quiz) end 我可以在quizzes / show.html.haml视图中显示每个问题 – @question.each do |question| = question.content 问题是上面的代码会在同一页面上显示所有问题。 我真正想要的是在每个问题中单独显示每个问题,每页一个问题。 因此,一旦用户回答了一个问题,他们就会点击一个按钮,它将使用下一个问题重新呈现视图。 无论如何,我有一段时间试图弄清楚如何做到这一点。 先感谢您! 哦,如果您需要查看我的代码,请告诉我!

编辑表单删除数据库中的记录

我有一个嵌套的表单,可以很好地保存数据库的内容。 唯一的问题是,当我单击编辑时,“grand-child”字段将从数据库中删除,因此我必须重新输入该字段上的所有内容。 正常行为是编辑应存在的先前内容。 我正在使用gem cocoon simple_forms用于我的表单的嵌套表单simple_forms和用于ckeditor编辑的ckeditor 。 这是我单击编辑时的服务器日志:通知: SQL (0.3ms) DELETE FROM “responses” WHERE “responses”.”id” = $1 [[“id”, 53]]每次我点击编辑时都会发生进入考试模型。 另请注意,问题已选中但未删除。 很奇怪。 Started GET “/exams/24/edit” for 127.0.0.1 at 2015-11-09 16:59:40 +0300 Processing by ExamsController#edit as HTML Parameters: {“id”=>”24”} Exam Load (0.8ms) SELECT “exams”.* FROM “exams” WHERE “exams”.”id” = $1 LIMIT 1 [[“id”, 24]] Question Load (0.4ms) […]

如何在不通过表单传递数据的情况下更新嵌套资源

楷模 class User :destroy, :inverse_of => :user has_many :companies, :through => :roles accepts_nested_attributes_for :roles, :limit => 1, :allow_destroy => true end class Role :roles belongs_to :company, :inverse_of => :roles accepts_nested_attributes_for :company end class Company :destroy, :inverse_of => :user has_many :users, :through => :roles validates :name, presence: true end 自定义设计注册控制器 class RegistrationsController < Devise::RegistrationsController # GET […]

属性一行,嵌套在下面(如何在表中显示正确的关系?)

这是我在开始Ruby on Rails旅程的过去几个月中遇到的最困难的问题。 似乎很多人都会面对它,但我在网上找不到解决这个问题的任何东西,这意味着我的问题要么是非常容易让我思考得太厉害,要么我提出错误的问题。 请帮助我找到正确的答案和/或正确的问题! 当用户提交表单时,它包含属性(已量化)和嵌套属性(结果)。 当用户点击索引时,我希望他看到量化在标题行中创建了一个新列(对于每个新的量化实例,它在页面上水平向右)。 然后,对于他提交或稍后添加的每个结果,我希望他看到结果在该列中创建一个新行(沿着页面垂直向下)。 我希望我解释得那么好,并没有让它听起来比它更令人困惑。 这是我可以使用html标签看起来像表格的一种变体: 这是另一种变化: 但无论出于何种原因,我无法得到这个,这就是我想要的: 我真的不知道在这一点上,因为我已经尝试了所有的东西,你需要什么代码来帮助我,所以不要把它全部放在这里我会通过github给你这里: https : //github.com/RallyWithGalli/ ruletoday UPDATE 使用下面的Patrick的答案,我能够让表格看起来像这样: 但它的问题是,如果用户添加另一个量化,例如meditate(min)并包含更多结果行然后在它之前的行,那么结果将向左移动,直到它到达具有那么多行或更多行的另一列。 在上面的例子中,“2.1”应该在冥想列中,而是落入重量列。 更新2.0 随着Patrick的更新,这就是现在的样子: 再次感谢Patrick尝试。 到目前为止,你是那个坚持我的男人。 你提到了bootstrap。 我再说一遍。 在此先感谢您的帮助! 我会永远欠你的债。

嵌套的simple_form,具有多态关联。 未经加工的参数

关于它的问题很多,但我找不到答案。 我有: group.rb class Group :describable accepts_nested_attributes_for :descriptions end description.rb class Description true end groups_controller.rb def update @group = Group.find(params[:id]) if @group.update_attributes(group_params) flash[:success] = “yes” redirect_to groups_path else render ‘edit’ end end private def group_params params.require(:group).permit(:owner_id, :domain, descriptions_attributes: [:id, :content]) end edit.html.erb 在控制台中我有Unpermitted parameter: description未创建Unpermitted parameter: description和嵌套属性。 我该怎么做才能最后保存?

Ruby on Rails:使用accepts_nested_attributes_for不会生成表单的一部分

我有两个模型:用户和个人资料。 我想使用相同的表单同时输入数据。 我正在跟踪关于嵌套模型表格(修订版)的关于转播196#。 问题是,表单的第一部分生成得很好。 这是第二部分(使用field_for),在视图中不显示。 我在stackoverflow中搜索了一个解决方案,并建议以某种方式使用“构建”操作。 但是,由于错误,这不起作用。 如果有人可以解释我如何使其发挥作用,我将非常感激。 我几天来一直在努力解决这个问题。 user.rb class User < ActiveRecord::Base attr_accessible :email, :first_name, :last_name, :orientation, :gender, :password, :password_confirmation, :date_joined, :last_online, :date_of_birth, :location, :profiles_attributes has_one :profiles accepts_nested_attributes_for :profiles, allow_destroy: true has_secure_password validates_confirmation_of :password #…..# end profile.rb class Profile < ActiveRecord::Base attr_accessible :height, :weight belongs_to :user end user/new.html.erb Form is invalid Date of […]

Rails 4:保存子记录ID但不保存其属性,使用带有has_many关系的嵌套表单

我有3个模型与has_many通关系:食物(例如:巧克力),Sub(巧克力食物替代品),关节(联合表)。 说@food = Food.find(1); has_many通过关系允许我做@subs = @ food.subs,它返回与@food相关的所有替代品。 这工作正常,但只保存Sub id而不是它的属性:name和:description,因为你可以看到它在我的控制器中的create action中尝试保存@ food.subs时返回nil: => #<ActiveRecord::Associations::CollectionProxy [#]> 我想问题在于我的食物控制器中的创建动作,也许与我的嵌套forms有关。 我花了无数个小时试图解决这个问题,我非常渴望找到答案。 我真的不知道在哪里看。 我是rails的新手,所以非常感谢你的帮助和时间,我真的很感激。 请尽可能根据我的初学者水平调整你的答案:-)。 下面是我的控制器样品,表格和相关信息。 这是我的模特: class Food :joints accepts_nested_attributes_for :subs end class Sub :joints accepts_nested_attributes_for :foods end class Joint < ActiveRecord::Base belongs_to :food belongs_to :sub end 这是我的db-schema FYI: create_table “foods”, force: true do |t| t.string “name” t.text “description” […]