我有点像 Rails 菜鸟,在解决问题时遇到了一些麻烦。
我正在使用带有以下 gem 的 Rails 3.2.13(除了默认 gem):
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 %>