我雇了一个oDesk老兄为我写了一些代码,但后来他消失了。我一直试图破译它,因为它不起作用,而且我只是一个Rails的爱好者,所以嵌套属性现在有点超出我的范围。
我有一个每个财政年度有12个期间的期限(红色,黄色,绿色)的市长。用户需要在每个句点结束时设置他的sla的period_state。我的创建/更新表单会抛出错误。
如果你能帮我解读发生的事情,我会欣赏正确方向的一点!我特别不知道更新方法中的p“ ** ”是什么......?如果你能提供帮助,请提前致谢 - 对于长篇文章感到抱歉。
class Sla < ActiveRecord::Base
has_many :period_states, :dependent => :destroy
accepts_nested_attributes_for :period_states, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
class PeriodState < ActiveRecord::Base
belongs_to :sla
class PeriodStatesController < ApplicationController
def index
@sla = Sla.find(params[:sla_id])
@period_states = @sla.period_states
#@periods = Period.where("fiscal_year = ?", @sla.fiscal_year)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @period_states }
end
end
def new
@sla = Sla.find(params[:id])
@period_states = @sla.period_states
@period_state = PeriodState.where("sla_id = ?", @sla.id)
@periods = Period.where("fiscal_year = ?", @sla.fiscal_year)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @period_state }
end
end
def create
@sla = Sla.find(params[:sla_id])
@period_states = @sla.period_states
if @period_states.size > 0
params[:sla][:period_states_attributes].each{|ps|
@period_state = PeriodState.find(ps[1][:id])
@period_state.period_state = ps[1][:period_state]
@period_state.save
}
respond_to do |format|
format.html { redirect_to(sla_period_states_path(@sla), :notice => 'Period state was successfully created.') }
end
else
respond_to do |format|
if @sla.update_attributes(params[:sla])
format.html { redirect_to(sla_period_states_path(@sla), :notice => 'Period state was successfully created.') }
format.xml { render :xml => @period_state, :status => :created, :location => @period_state }
else
format.html { render :action => "new" }
format.xml { render :xml => @period_state.errors, :status => :unprocessable_entity }
end
end
end
end
def update
p "****************************"
p params[:sla][:period_states_attributes].reject_if { |attrs| attrs["state_id"].blank? }
p"**********************"
@sla = Sla.find(params[:sla_id])
params[:sla][:period_states_attributes]
respond_to do |format|
if @sla.update_attributes(params[:sla])
format.html { redirect_to(sla_reports_path(@sla), :notice => 'Period state was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @sla.errors, :status => :unprocessable_entity }
end
end
end
period_state / new.html
<% if @period_state.size > 0 %>
<%= render 'edit' %>
<% else %>
<%= render 'form' %>
<% end %>
period_state / _edit.html
<table>
<tr>
<th>Period</th>
<th>State</th>
</tr>
<% @periods.each_with_index do |p, i| %>
<%#= p.id %>
<% @period_state = PeriodState.find_by_sla_id_and_period_id(@sla.id, p.id) %>
<%#= p.inspect %>
<%= fields_for "sla[period_states_attributes][#{i.to_i}]", @period_state do |ps| %>
<tr>
<td><%= p.pe_number %> <%= ps.hidden_field :period_id, :value => p.id %> </td>
<td><%= ps.collection_select(:state_id, State.all, :id, :state, :order => "state" , :include_blank => true ) %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
<p>
<div class="actions">
<%= f.submit "Enter/Edit Period States" %>
</div>
period_state / _form.html
<table>
<tr>
<th>Period</th>
<th>State</th>
</tr>
<% @periods.each_with_index do |p, i| %>
<%= fields_for "sla[period_states_attributes][#{i.to_i}]", PeriodState.new do |ps| %>
<tr>
<td><%= p.pe_number %> <%= ps.hidden_field :period_id, :value => p.id %> </td>
<td><%= collection_select(:period_state, :state_id, State.all, :id, :state, :order => "state" , :include_blank => true ) %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
<p>
<div class="actions">
<%= f.submit "Enter/Edit Period States" %>
</div>