1
我僱了一位oDesk哥們爲我寫了一些代碼,但後來他消失了。我一直試圖破譯它,也就是因爲它不起作用,而且我只是一個Rails愛好者,所以嵌套的屬性現在有點超出我的範圍。解密導軌3代碼?
我有一個sla的period_state(紅色,黃色,綠色),每個財政年度有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>