2013-07-26 3 views
2

저는 레일스 놈입니다. 문제 해결에 어려움을 겪고 있습니다.best_in_place와 중첩 된 속성이있는 레일 3 문제

내가 (기본 보석 외에) 다음과 같은 보석과 레일 3.2.13을 사용하고 있습니다 :

gem 'devise' 
gem 'cancan' 
gem 'cocoon' 
gem 'best_in_place' 
나는이 경우, 중첩 된 모델 작업을 고치를 사용하고

내가 가지고 (고안) 사용자가 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 : https://github.com/bernat/best_in_place/issues/51 이에 따라

<% @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 %> 
+0

업데이트 할 때 params []에있는 내용은 무엇입니까? 어쩌면 best_in_place가 잘못된 매개 변수를 보내고 있습니다. params [: project]와 update_attributes (params [: project])에 아무 것도 없을 것입니다. – stef

답변

0

best_in_place 지원하지 않습니다.

보석 작성자는 장소 편집에서 한 모델의 속성 만 수정해야하므로 다른 모델의 속성을 변경하는 것이 범위에 포함되지 않는다고 주장합니다.

내 의견 : 개인적으로 나는 그 결정을 후회합니다.

1

@Ich가 지적한대로이 사용은 공식적으로 지원되지 않지만 임시 해결 방법이 있습니다. 추가 컨트롤러가 필요 없습니다. best_in_place에 param 매개 변수를 전달하면됩니다. 그것은하는 Car에 대한 약간 더 복잡 것을

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]" 

참고하는 has_many :wheels (또는에 대한 Unicycleupdate_only 당신을 위해 작동하지 않습니다 has_one :wheel 그).

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에서 작동하며 이전 버전에서도 가능했지만 테스트하지 않았습니다.