Tag: 嵌套属性

使用selectbox nested_form gem编辑模型

我有一个嵌套的表单gem问题,几天都无法搞清楚。 当“编辑”模型时,为什么我的选择框没有从数据库中填充当前值? 我的“自定义”视图: create_customize_cart_path do |f| %> # some field here builder %> 我的局部视图(嵌套): ‘– Select Position –‘} %> PRESSED POSITION为CONSTANT ,数据存储“字符串”值 PRESSED_POSITION = [ [“Top”, “top”], [“Center”,”center”], [“Bottom”,”bottom”], [“Right”, “right”], [“Left”, “left”], [“Top Left”, “top left”], [“Top Center”, “top center”], [“Top Right”, “top right”], [“Center Left”, “center left”], [“Center Center”, “center center”], [“Center Right”, […]

ActiveAdmin不保存has_many嵌套对象

我似乎无法找出为什么我的has_many记录不会保存。 我在模型中使用has_one进行测试并且它工作正常,但是当我将其更改为has_many它不起作用。 我检查了几个类似的stackoverflowpost,我似乎有这个正确,但它不起作用。 activeadmin中的嵌套表单不保存更新和ActiveAdmin表单不保存嵌套对象 这适用于Rails version 5.1.0.alpha和Activeadmin version 1.0.0.pre4 我没有得到任何错误,也没有看到嵌套对象被创建虽然我在这里创建一个对象时看到params是输出 Started POST “/admin/abq_districts” for ::1 at 2016-12-05 10:04:44 -0700 Processing by Admin::AbqDistrictsController#create as HTML Parameters: {“utf8″=>”✓”, “authenticity_token”=>”bHzfUG8xIMI66tTHNwSmL5FVnaa4nyuzJ3AM8tnLJSq69TLP1o8iUcLLeNnoS0FVgA8ju3x7Ioc+EV4xRv/T7Q==”, “abq_districts_district”=>{“name”=>”test district”, “title”=>””, “sub_title”=>””, “paragraph”=>””, “sections_attributes”=>{“0″=>{“text”=>”test section”}}}, “commit”=>”Create District”} AdminUser Load (1.0ms) SELECT “admin_users”.* FROM “admin_users” WHERE “admin_users”.”id” = $1 ORDER BY “admin_users”.”id” ASC LIMIT $2 [[“id”, 1], […]

使用嵌套属性时的批量分配警告

我对rails上的编程和ruby都很陌生。 我已经关注了http://ruby.railstutorial.org/然后我开始观看http://railscasts.com的剧集。 我想要做的是“以单一forms处理多个模型”。 下面你将看到我的模型和他们的assosications以及我试图从用户获取信息的表单视图。 我的建模是那样的; 有雇主,雇主有面试和面试有问题。 Customquestion模型: class Customquestion < ActiveRecord::Base attr_accessible :content belongs_to :interview validates :content, length: {maximum: 300} validates :interview_id, presence: true end 面试模型: class Interview < ActiveRecord::Base attr_accessible :title, :welcome_message belongs_to :employer has_many :customquestions, dependent: :destroy accepts_nested_attributes_for :customquestions validates :title, presence: true, length: { maximum: 150 } validates :welcome_message, presence: true, length: […]

如何让nested3中的nested_attributes工作?

这是我的用户模型: class User :destroy accepts_nested_attributes_for :teacher, :allow_destroy => true attr_accessible :email, :password, :password_confirmation, :remember_me, :teacher_attributes end 这是我的老师模型: class Teacher < ActiveRecord::Base belongs_to :user attr_accessible :user_id, :first_name, :last_name validates_presence_of :user_id, :first_name, :last_name end 这是我的表格: registration_path(:user)) do |user| %> 除了这个东西不会“接受嵌套属性”我的开发日志说: WARNING: Can’t mass-assign protected attributes: teacher 我不知道它是否相关,但表单不是在teacher_attributes数组中生成字段或任何东西 – 它在教师内部。 我猜这是我的问题所在,但我不知道如何把它放在里面。 请帮忙。 谢谢!

.build无法分配尚不存在的外键

我有以下代码为我的用户控制器中的用户创建图片: def new @user = User.new @pictures = @user.pictures.build({:placement => “front”},{:placement => “profile”}) end 但是,当我创建新用户时,它不会自动构建具有“前”或“配置文件”位置的图片。 事实上,图片没有任何更新。 图片模型有 attr_accessible :placement 并在picture.rb belongs_to :user 并在user.rb中 has_many :pictures accepts_nested_attributes_for :pictures, allow_destroy: true 为什么我的构建命令无声地失败? UPDATE 使用.save!,我了解到正在分配@pictures ,但问题是没有user_id与它们相关联。 当我故意分配user_id时,如: def new @user = User.new @pictures = @user.pictures.build({:placement => “front”, :user_id => @user.id},{:placement => “profile”, :user_id => @user.id}) end 它仍然没有分配user_id。 奇怪的是,当我在rails控制台中运行相同的命令时,它会正确分配 […]

ActiveRecord :: Relation(嵌套属性)的未定义方法

结果:: ActiveRecord_Relation的未定义方法`results’:0x007f9ee69ad148> Quantifieds#指数 这里的第一行是导致exception的原因。 要重述这个问题,我如何定义date_value和result_value以使其在quantieds索引中起作用? 它们来源于:_form中的结果。 我正在使用cocoon来创建嵌套属性。 整个索引 (根据第一个答复响应更新) AVERAGE () INSTANCE () <span class="glyphicon glyphicon-plus" result %> [:month, :year], class: ‘date-select’ %> 我需要更改控制器中的任何内容吗? class QuantifiedsController < ApplicationController before_action :set_quantified, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] def index @quantifieds = Quantified.joins(:results).all @averaged_quantifieds = current_user.quantifieds.averaged @instance_quantifieds = current_user.quantifieds.instance end def show end […]

非资源嵌套的命名路由

我正在尝试生成一个用户点击确认其帐户的链接。 我想要这个: /users/:id/confirm/:code 我在我的路线文件中有这个: resources :users do member do get ‘confirm/:confirmation_code’, :action => ‘confirm’ end end 我试过了: user_confirm_path(@user, @confirmation_code) confirm_user_path(@confirmation_code, @user) 和其他许多人一样,但似乎无法找到正确的人。 我想我总是可以自己生成链接url,但这似乎不是轨道方式。 这是我的rake路线输出: rake routes Prefix Verb URI Pattern Controller#Action GET /users/:id/confirm/:confirmation_code(.:format) users#confirm 但是省略了我正在寻找的东西

如何在控制器方法中设置Rails 4中嵌套参数的值?

当我创建新的请求对象时,我有请求模型和嵌套模型filled_cartridges我还需要在create controller方法中设置嵌套params的值。 我有很多嵌套参数,所以我需要迭代它们,类似的东西: params[:request][:filled_cartridges_attributes].each do |_,value| # here i try to set :client_id parameter, which is nested # where @client.id is defined value[:client_id] = @client.id # i am pretty sure that the problem is here # is it a correct way of ding that? end 编辑: has_many :filled_cartridges, inverse_of: :request, dependent: :destroy accepts_nested_attributes_for :filled_cartridges, reject_if: […]

插入的命名路由。 代替 /

我在rails 4中的命名路由遇到了问题( 非资源嵌套的命名路由 )。 我已经转移到其他东西,但仍然在努力解决非资源url的命名路由问题。 这是我从rake routes : GET /messages/:id/report/:reply_token(.:format) messages#report messages POST /messages(.:format) messages#create 和我的routes.rb resources :messages, only: [:create] do member do get ‘report/:reply_token’, :action => ‘report’#, :as => :message end end 由于我在顶部链接的post中遇到的问题,我正在尝试通过执行以下操作获取/ messages /:id / report /:reply_token路由的URL: “#{messages_url(@message, :host => “localhost:3000″)}/report/#{@message.reply_token}” 但它给了我这个: http://localhost:3000/messages.110/report/6bBw22TdaRYcQ3iVzW1ZwA 为什么会有。 在’messages’和’110’(message_id)之间? 我还在messages_url()尝试了@ message.id,而不是@message。 我也尝试了这个: report_message_path(message_id: @message.id, reply_token: @message.reply_token)但是得到了与上面链接的问题相同的错误。 我也尝试过message_url()但它给出了undefined […]

嵌套属性不保存在数据库中? 不显示值 – Ruby on Rails

我有一个嵌套属性,我可以显示值,但它不保存到数据库中为什么? 我有一个模型review ,我想嵌套属性comments 查看迁移文件 class CreateReviews < ActiveRecord::Migration def change create_table :reviews do |t| t.belongs_to :reviewable, polymorphic: true t.timestamps end add_index :reviews, [:reviewable_id, :reviewable_type] end end 审查模型 class Review < ActiveRecord::Base attr_accessible :rating, :user_id, :comments_attributes, :service has_many :comments, as: :commentable belongs_to :user belongs_to :reviewable, polymorphic: true accepts_nested_attributes_for :comments end 评论迁移文件 class CreateComments < ActiveRecord::Migration def […]