ruby-on-rails - 在表单中设置has_one关系,但获取对象与字符串异常

标签 ruby-on-rails ruby ruby-on-rails-3 associations simple-form

我的模型中有一个 has_one 关系(组织有一个模板),并且正在尝试通过表单更新它。但是,当我这样做时,我收到以下错误:

ActiveRecord::AssociationTypeMismatch in OrganizationsController#update

Template(#70209323427700) expected, got String(#70209318932860)

这有点复杂,因为每个组织都有许多与其关联的模板,但只有一个当前模板,如模型所示:

class Organization < ActiveRecord::Base
  validates :subdomain, :presence => true, :uniqueness => true
  validates :current_template, :presence => true


  has_many :organization_assignments
  has_many :people
  has_many :pages
  has_many :templates
  has_many :users, :through => :organization_assignments
  has_one :current_template, :class_name => 'Template'


  attr_accessible :name, :subdomain, :template_id, :current_template, :current_template_id
end

这是我的表格:

= simple_form_for @organization, :html => { :class => 'form-horizontal' } do |f|
  - @organization.errors.full_messages.each do |msg|
    .alert.alert-error
      %h3
        = pluralize(@organization.errors.count, 'error')
        prohibited this organization from being saved:
      %ul
        %li
          = msg

  = f.input :name

  = f.input :subdomain
  = f.input :current_template, :collection => @organization.templates, :selected => @organization.current_template

  .form-actions
    = f.submit nil, :class => 'btn btn-primary'
    = link_to t('.cancel', :default => t("helpers.links.cancel")), organizations_path, :class => 'btn'

为了更好地衡量,我的 Controller :

  def update
    @organization = Organization.find(params[:id])

    respond_to do |format|
      if @organization.update_attributes(params[:organization])
        format.html { redirect_to @organization, notice: 'Organization was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @organization.errors, status: :unprocessable_entity }
      end
    end
  end

我尝试过使用嵌套表单:

  = simple_fields_for :current_template do |f|
    = f.input :current_template, :collection => @organization.templates, :selected => @organization.current_template

但成功所做的只是更改 ID 号,而没有实际更改关联的表单。我错过了什么?

最佳答案

问题在于 params[:organization][:template] 的值是包含所选模板 ID 的字符串。您需要使用该 ID 查找 Template 的实际实例并分配给 params[:organization][:template]。例如:

def update
  @organization = Organization.find(params[:id])
  if (params[:organization])
    params[:organization][:template] = Template.find(params[:organization].delete(:template))
  end

  respond_to do |format|
    if @organization.update_attributes(params[:organization])
    # ...
  end
end

关于ruby-on-rails - 在表单中设置has_one关系,但获取对象与字符串异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11714504/

相关文章:

ruby-on-rails - 如何在Rails Controller 中处理ActiveRecord::RecordNotFound?

ruby-on-rails - 在 webpacker react 应用程序中读取 rails 加密凭据

html - Ruby on Rails CSS 未通过 Assets 管道加载

ruby-on-rails-3 - 包括外部 gem 在内的完整 Rails 应用程序备份?

ruby-on-rails - "where"和 "find"之间的区别

ruby-on-rails - mongoid 模型的 key 不起作用

ruby - ruby 中 Array#reject 的反义词是什么?

ruby-on-rails - 注销前更新 current_user 时收到 "Attempted to update a stale object: User"异常

ruby-on-rails - 将应用程序推送到 heroku 问题

ruby-on-rails - Rails 3 上嵌套路由的功能测试