내 앱에 패키지가 많이 있고 (중첩 속성을 허용하는) 동물 모델이 있습니다. 내 Animals Controller에서 일반 동물 편집에 사용 된 것과 다른 페이지 (및 별도의 사용자 역할)의 특정 Animal (및 중첩 된 패키지) 특성을 편집 할 수 있도록 다음 작업을 만들었습니다. (명확히하기 위해, 내가 이상적으로 다른 사용자 역할이 동물에 대해 다른 일을 편집 할 수 동물 # 편집 작업 페이지, 별도의 동물 # 로그 행동과 페이지를 가지고) :레일 사용자 정의 중첩 업데이트가 업데이트되지 않음
def log
@animal = Animal.find(params[:animal_id])
if params[:animal]
if @animal.update_attributes(params[:animal])
# I want a basic reload (with flash) in cases of both success and failure.
redirect_to log_path(@animal), notice: "Animal update successful!"
else
redirect_to log_path(@animal), notice: "Update unsuccessful. Contact admin"
end
end
end
내 별도의 편집/업데이트 행동 : 나는 (코드 아래) 내 로그 페이지 내 제출 버튼을 누르면 때
def update
@animal = Animal.find(params[:id])
if @animal.update_attributes(params[:animal])
redirect_to @animal, notice: "Animal was successfully updated."
else
render action: "edit"
end
end
def edit
@animal = Animal.find(params[:id])
end
이 문제는이 페이지를 새로 고침하지만,하는) 어떤 어떤 종류의 (사소한 문제)의 통지와 b없이) 더 중요하게, 패키지 정보를 실제로 업데이트하지 않아도됩니다. 중요한 것은 animal.weight 특성 (여기서 수정할 수있는 유일한 ANIMAL 특성)을 업데이트하면 작동한다는 것입니다. 중첩 된 패키지 속성이 아닙니다.
도움 말? 나는 정말로 여기에서 곤란하다. 나는 이것을 영원히 작동 시키려고 노력하고있다. 아래에 몇 가지 가능성 관련 코드 : (동물 자원 위) 내 routes.rb 파일에서
:
match '/animals/:animal_id/log' => "animals#log", as: :log
여기에 내 로그 뷰의 :
<%= form_for(@animal, :url => { :action => "log" }, :method => :put) do |f| %>
<div style="width: 200px">
<% if @animal.weight %>
<%= "Weight: #{@animal.weight}lbs" %>
<% else %>
<%= f.label "Weight (in lbs)" %>
<%= f.text_field :weight %>
<% end %>
</div>
# I'm cycling through each bundle to group "identical" packages in a table (one per bundle).
<% @animal.sold_bundles.each do |bundle| %> <!-- Packages are bundled by cut and prep notes -->
<%= render partial: 'package_bundle', locals: {:bundle => bundle, :f => f} %><br>
<% end %>
<%= f.submit "Submit Animal Log", :class => "btn image-button right" %>
<% end %>
그리고 여기에 "package_bundle"부분을 요구입니다 각 번들. 그것은 패키지의 테이블을 생성, 일부 편집 (때문에 빈 true_weight 값) 및 일부하지 :
<table class="table">
<thead>
<tr>
<th>Cut Name</th>
<th>Prep Notes</th>
<th>Label</th>
<th>Actual Lbs</th>
<th>Actual Oz</th>
</tr>
</thead>
<tbody>
<!-- list of specified packages -->
<%= f.fields_for :packages, bundle do |p| %>
<tr>
<td><%= p.object.name %></td>
<td><%= "#{p.object.user.name.first(3).upcase}-#{p.object.id}" %></td>
<% if p.object.true_weight == nil || p.object.true_weight == 0 %>
<td colspan=1><%= p.text_field :actual_lbs %></td>
<td colspan=1><%= p.text_field :actual_oz %></td>
<% else %>
<td><%= p.object.true_weight.round(0) %> lb</td>
<td><%= ((p.object.true_weight % 1) * 16).round(2)%> oz </td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
편집 - 요청에 대한 더 많은 코드
package.rb (단지 관련 기능) - 나는 회피 아래에 잡았다 (이번에, 나는 확실히 그 실수를하기 전에). 그것은 퍼센트와 파운드 파운드/온스를 변환 내 방법은 여기를 비난하는 것입니다 가능성,하지만 그것은 바로 내 보이는 :
class Package < ActiveRecord::Base
attr_accessible :animal_id, :cut_id, :price, :line_id, :sold, :savings, :actual_lbs, :actual_oz, :true_weight
attr_accessor :actual_lbs, :actual_oz
belongs_to :animal
belongs_to :cut
belongs_to :line
before_update :to_true
def to_true
if self.sold
if self.order.status > 1 # This is the case for any package loaded on the log page
if self.actual_lbs && self.actual_oz
unless self.true_weight && self.true_weight > 0 # Don't overwrite legit true weights
percent = self.actual_oz/16
self.update_attribute(:true_weight, self.actual_lbs + percent)
end
end
end
end
end
animal.rb (에만 해당) :
class Animal < ActiveRecord::Base
attr_accessible :breed, :name, :photo, :animal_type, :hanging_weight, :meat_weight,
:weight, :ranch_id, :butcher_id, :cow_mult, :pig_mult, :packages_attributes,
:lamb_mult, :goat_mult, :host_id, :final_sale, :opening_sale, :open, :no_sales
has_many :orders
has_many :packages
belongs_to :butcher
belongs_to :ranch
belongs_to :host
after_create :create_packages
accepts_nested_attributes_for :packages
보았다 내 서버 로그에서 (개발자 로그보다 이해하기가 더 쉽다),이 밑바닥까지 가야하고, 이상한 두 가지 사실을 알 수있다. 오류는 전혀 없지만 1)로드 할 때 페이지에 대한 GET을 실행하는 것처럼 보이지만 제출을 클릭했을 때 요청한 PUT에는 해당하지 않습니다. 2) 아마도 결과로 유일한 매개 변수는 전달 된 것입니다. GET 요청의 일부)은 동물 ID입니다. 패키지 속성은 전달되지 않습니다.
그냥 그렇게했습니다. 응답 주셔서 감사합니다! 나는 SO 응답이 늦어 질 때까지 부정적인 반응을 보였습니다. 어쩌면 내 질문이 점점 더 이상해지고있는 것 같습니다. – Sasha
그래서 @Sasha -이 답변을 귀하의 질문에 했습니까? 그렇다면 허용 된 것으로 표시 할 수 있습니까? 그렇지 않다면,'animal.rb' 클래스가 필수': packages_attributes' 접근자를 가지고있는 것처럼 보이지만, 아마도'accept__ested_attributes_for'가 자동으로 수행하는 작업을하고있는'after_create' 콜백 사이에 충돌이있을 것입니다. development.log에서 실제로 발생하는 것을 확인하십시오. –
글쎄, 나는 위의 문제를 해결하지 못했습니다. after_create 콜백은이 특별한 경우에는 사용되지 않습니다. 그것은 다른 페이지에서 (성공적으로) 사용되어 동물과 관련된 패키지 객체를 만들고 동물을 만들 때 특정 속성을 사용합니다. 그리고 내가 packages_attributes 행을 가지고 있기 때문에 나는 아직도 어떤 일이 일어나고 있는지 전혀 모른다. 내가 볼 개발 로그에 다이빙하려고 해요. – Sasha