我試圖在名爲Interventions的連接表中創建一條記錄。基本上在我的應用程序中,用戶可以對事件進行多種干預,事件可以由許多用戶進行干預。我傳遞了所需的強參數,但是當我試圖保存時,應用程序會出現以下錯誤:「事件必須存在」和「用戶必須存在」。我花了數小時的時間,並無法弄清楚問題所在。你能幫我麼?我張貼相關的代碼在這裏:Rails加入收集不識別參數
user.rb (model)
has_many :interventions
has_many :incidents, through: :interventions
incident.rb (model)
has_many :interventions
has_many :users, through: :interventions
intervention.rb (model)
belongs_to :incident_priority
belongs_to :intervention_status
interventions_controller.rb
def new
@incident = Incident.find(params[:incident])
@user = User.find(current_user.id)
@intervention = Intervention.new(:user_id => @user, :incident_id => @incident)
@project = @incident.channel.project
@mirth = Mirth.find_by server_id: @incident.mirth_server_id
end
def create
@incident = Incident.find(params[:incident_id])
@user = User.find(params[:user_id])
@intervention = Intervention.create(intervention_params)
@project = @incident.channel.project
@mirth = Mirth.find_by server_id: @incident.mirth_server_id
respond_to do |format|
if @intervention.save
format.html { redirect_to new_intervention_path(@incident), notice: 'Intervention was successfully created.' }
format.json { render :show, status: :created, location: @intervention }
else
format.html { render :new, incident: :incident_id }
format.json { render json: @intervention.errors, status: :unprocessable_entity }
end
end
end
<....>
def intervention_params
params.require(:intervention).permit(:user_id, :incident_id, :incident_priority_id, :begin_date, :end_date, :description,
:intervention_status_id, :forwarded_to)
end
在我看來(interventions_form.html.erb):
<%= form_for(@intervention) do |f| %>
<% if @intervention.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@intervention.errors.count, "error") %> prohibited this intervention from being saved:</h2>
<ul>
<% @intervention.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group form-inline">
<%= hidden_field_tag :user_id, @user.id %>
<%= hidden_field_tag :incident_id, @incident.id %>
<strong>Interveniente:</strong>
<%= @user.first_name %> <%= @user.last_name %>
</div>
<div class="form-group form-inline">
<%= f.label 'Prioridade' %>
<%= f.collection_select :incident_priority_id, IncidentPriority.all, :id, :description, {}, {class: "form-control"} %>
</div>
<div class="form-group form-inline">
<%= f.label 'Data início intervenção' %>
<%= f.datetime_select :begin_date %>
</div>
<div class="form-group form-inline">
<%= f.label 'Data fim intervenção' %>
<%= f.datetime_select :end_date, :include_blank => true %>
</div>
<div class="form-group form-inline">
<%= f.label 'Observações' %>
<%= f.text_area :description %>
</div>
<div class="form-group form-inline">
<%= f.label 'Estado' %>
<%= f.collection_select :intervention_status_id, InterventionStatus.all, :id, :description, {}, {class: "form-control"} %>
</div>
<div class="form-group form-inline">
<%= f.label 'Encaminhado para:' %>
<%= f.text_area :forwarded_to %>
</div>
<div class="actions" align="right">
<%= link_to 'Voltar', incidents_path(:mirth => @mirth, :project => @project), class: "btn btn-info" %>
<%= f.submit "Gravar", class: "btn btn-info" %>
</div>
我運行調試和值在hidden_field_tags中正確填充。同樣在控制器中,@user和@incident已正確填充,但@intervention在外鍵中沒有:user_id和:incident_id。
在此先感謝!
設置'這裏user_id'當'@intervention = Intervention.new(:USER_ID => @user ,:incident_id => @incident)'@ user.id'或者簡單地設置'user:@ user'同樣適用於事件。看看是否有效 – Abhinay
我做了這個改變,但它給出了同樣的錯誤:/ – Ayanami
對這個問題的回答基本上與我所建議的相同。 :)很高興它的工作。 – Abhinay