Ruby on Rails - 将id从模型传递到另一个模型

时间:2014-05-29 01:50:20

标签: ruby-on-rails-4 global-variables

我有两个模型,AppointmentIntervention(预约属于干预和介入已经预约)

Appointment的索引视图中,我有一个指向new_Intervention_path

的链接

如果创建了新干预,我想将Appointment的状态设置为DONE(以前是PENDING),并将新干预的ID分配给appointment.intervention_id字段。

如何将约会的ID传递给干预控制器,以便我能够实现这一目标?

- EDIT ---

这是模型Appointment

的索引视图
<h1>Listing appointments</h1>

<table>
 <thead>
   <tr>
    <th>Mes</th>
    <th>Ano</th>
    <th>Agendado</th>
    <th>Observacoes</th>
    <th>Addendum</th>
    <th>Intervention</th>
    <th>Appointment state</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>

<tbody>
  <% @appointments.each do |appointment| %>
    <tr>
      <td><%= appointment.dataprovavel.mon %></td>
      <td><%= appointment.dataprovavel.year %></td>
      <td><%= appointment.dataagendado %></td>
      <td><%= appointment.observacoes %></td>
      <td><%= appointment.addendum.numero %></td>
      <td><%= appointment.intervention_id %></td>
      <td><%= appointment.appointment_state.descricao %></td>
      <td><%= link_to 'Show', appointment %></td>
      <td><%= link_to 'Edit', edit_appointment_path(appointment) %></td>
      <td><%= link_to 'Destroy', appointment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      <td><%= link_to 'New Intervention', new_intervention_path%></td>
    </tr>
  <% end %>
</tbody>
</table>

我添加了link_to 'New Intervention', newintervention_path

这是Intervention控制器 注意:这是代码错误,我只是想解释一下我提出的内容

class InterventionsController < ApplicationController
  def create
@intervention = Intervention.new(intervention_params)

respond_to do |format|
  if @intervention.save
    format.html { redirect_to @intervention, notice: 'Intervention was successfully created.' }
    format.json { render action: 'show', status: :created, location: @intervention }
    updateAppointment
  else
    format.html { render action: 'new' }
    format.json { render json: @intervention.errors, status: :unprocessable_entity }
  end
end
end

def updateAppointment
  Appointment.find(appointment_id).appointment_state_id = 1
end

我希望附录控制器上使用的变量appointment_id存储约会的索引视图中的约会ID

1 个答案:

答案 0 :(得分:0)

我设法找到问题的答案,只是不知道它是否是最好的问题。

在视图Appointment/index.html.erb上,我添加了以下link_to

<td><%= link_to 'New Intervention', new_intervention_path(:appointment_id => appointment.id)%></td>

我通过网址

发送了appointment_id

在新干预的形式上(interventions\_form.html.erb),我添加了一个隐藏的字段,如:

<%= f.hidden_field :appointment_id, :value => params[:appointment_id]%>

关于干预控制器interventions_controller.rb的动作创建我添加了以下代码:

def create

@appointment = Appointment.find(params[:intervention][:appointment_id])
@intervention = Intervention.new(intervention_params)

respond_to do |format|
  if @intervention.save
    format.html { redirect_to @intervention, notice: 'Intervention was successfully created.' }
    format.json { render action: 'show', status: :created, location: @intervention }
    activateAppointment
  else
    format.html { render action: 'new' }
    format.json { render json: @intervention.errors, status: :unprocessable_entity }
  end
end
end

def activateAppointment
  @appointment.appointment_state = AppointmentState.where(description:'Done').first
  @appointment.intervention_id = @intervention.id
  @appointment.save
end