【问题标题】:Rails 3 issue with best_in_place and Nested Attributes关于 best_in_place 和嵌套属性的 Rails 3 问题
【发布时间】:2013-07-27 05:45:23
【问题描述】:

我是一个 Rails 菜鸟,但在解决某个问题时遇到了一些麻烦。

我正在使用带有以下 gem(除了默认 gem)的 Rails 3.2.13:

gem 'devise'
gem 'cancan'
gem 'cocoon'
gem 'best_in_place'

我正在使用 cocoon 来处理嵌套模型,在这种情况下,我有一个(设计)用户,他负责 has_many 项目和每个项目 has_many 任务。

我可以通过以下方式获取所有要显示的信息(并在点击时变为可编辑):

<% @project.tasks.each do |task| %>
<%= best_in_place task, :description, :path => tasks_path, :type => :input %>
<% end %>

我的问题是 我无法使用 best_in_place 保存对嵌套任务属性的更新

项目模型

class Project < ActiveRecord::Base
  belongs_to :user

  has_many :tasks

  attr_accessible :description, :name, :tasks_attributes, :user_id, :tasks
  accepts_nested_attributes_for :tasks, :reject_if => :all_blank, :allow_destroy => true
end

任务模型

class Task < ActiveRecord::Base
  belongs_to :project

  attr_accessible :description, :done, :hours
end

项目控制器

class ProjectsController < ApplicationController
  before_filter :authenticate_user!

  def show
    @project = Project.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @project }
    end
  end

def update
  @project = Project.find(params[:id])

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

项目 -> show.html.erb:

<% @project.tasks.each do |task| %>
  <li class="module-list-item ui-state-default clear">
    <section class="task-name left">
      <%= best_in_place task, :description, :path => tasks_path, :type => :input %>
    </section>
  </li>
<% end %>

【问题讨论】:

  • 更新时参数[] 中有什么内容?也许 best_in_place 发送了错误的参数,params[:project] 中可能没有任何内容,并且 update_attributes(params[:project]) 失败

标签: ruby-on-rails ruby-on-rails-3 devise best-in-place


【解决方案1】:

虽然官方不支持这种用法(正如@Ich 指出的那样),但有一种解决方法。不需要任何额外的控制器。您只需将 param 参数传递给 best_in_place。

class Unicycle < AR::Base
  has_one :wheel
  accepts_nested_attributes_for :wheel, update_only: true
end

best_in_place unicycle.wheel, :last_rotation, 
  path: unicycle_path(unicycle.id), type: :input, 
  param: "unicycle[wheel_attributes]"

请注意,对于 Carhas_many :wheels(或者对于 Unicyclehas_one :wheel,其中 update_only 不适合您),它会稍微复杂一些。

car.wheels.each do |wheel|
  best_in_place wheel, :last_rotation, 
    path: car_path(car.id), type: :input, 
    param: "car[wheel_attributes][id]=#{wheel.id}&car[wheel_attributes]" 
    # Note that you have to pass the ID of the wheel in this case
end

适用于 v3.0.3,也可能适用于更早的版本,但我尚未测试。

【讨论】:

    【解决方案2】:

    根据https://github.com/bernat/best_in_place/issues/51,best_in_place 不支持。

    gem 的作者认为,就地编辑应该只修改一个模型的属性,因此更改另一个模型的属性不在范围内。

    我的意见:我个人对这个决定感到遗憾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多