Rails加入集合而不识别参数

时间:2016-10-10 15:39:22

标签: ruby-on-rails

我试图在名为Interventions的连接表中创建记录。基本上在我的应用程序中,用户可以对事件进行许多干预,并且事件可以由许多用户进行干预。我传递了所需的强参数,但是当我尝试保存时,应用程序会出现以下错误:"事件必须存在"和"用户必须存在"。我花了好几个小时,但无法弄清问题是什么。你能帮我么?我在这里发布相关代码:

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>

我运行debug并正确填充hidden_​​field_tags中的值。同样在控制器中@per和@incident被正确填充,但@intervention在外键中没有nil:user_id和:incident_id。

提前致谢!

2 个答案:

答案 0 :(得分:0)

你需要改变这个:

def create
  @incident = Incident.find(params[:incident_id])
  @user = User.find(params[:user_id])

  # ...
end

为此:

def create
  @incident = Incident.find(params[:incident_id])
  @user = User.find(params[:user_id])
  intervention_params.merge(user_id: @user.id, incident_id: @incident.id)

  # ...
end

你的模特:

class Intervention
  belongs_to :user
  belongs_to :incident

答案 1 :(得分:0)

您需要将用户和事件与干预相关联。

在您的模型中......

class Intervention
  belongs_to :user
  belongs_to :incident

在您的创建方法中......

def create
  @incident = Incident.find(params[:incident_id])
  @user = User.find(params[:user_id])
  @intervention = Intervention.create(intervention_params)
  @intervention.incident = @incident
  @intervention.user = @user
  ...