0

Estoy tratando de hacer funcionar un recurso anidado pero me quede atascado; obtengo un error al tratar de crear una nueva 'prescripcion', por favor si alguien puede ayudarme a identificar lo que esta sucediendo se lo agradecería.

Saludos!

Error: undefined method `new' for nil:NilClass

def new
  @prescription = @prescription.new
end

Error

Rutas:

Rails.application.routes.draw do

  resources :patients, shallow: true do
    resources :consultations do
      resources :prescriptions
    end 
  end
end

Vista desde la cual se genera '/prescriptions/new'

<%= link_to 'New Prescription', new_consultation_prescription_path(@consultation) %>
<%= link_to 'Show Prescriptions', consultation_prescriptions_path(@consultation) %> 

Controlador 'Prescriptions':

class PrescriptionsController < ApplicationController
  before_action :set_consultation
  before_action :set_prescription, only: [:show, :edit, :update, :destroy]

  def index
    @prescription = Prescriptions.all
  end

  def show
  end

  def new
    @prescription = @prescription.new
  end

  def edit
  end

  def create
    @prescription = @prescription.new(prescription_params)

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

  private
    def set_consultation
      @consultation = Consultation.find_by_id(params[:id])
    end

    def set_prescription
      @prescription = Prescription.find(params[:id])
    end

    def prescription_params
      params.require(:prescription).permit(:prescription, :consultation_id)
    end
end

Modelo 'Prescription':

class Prescription < ActiveRecord::Base
  belongs_to :consultation, :dependent => :destroy
end

Form 'Prescription':

<%= form_for([@consultation, @prescription]) do |f| %>
  <% if @prescription.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@prescription.errors.count, "error") %> prohibited this prescription from being saved:</h2>

      <ul>
      <% @prescription.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :prescription %><br>
    <%= f.text_area :prescription %>
  </div>
  <div class="field">
    <%= f.label :consultation_id %><br>
    <%= f.text_field :consultation_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 respuesta 1

0

La forma que usas para inicializar el objeto de prescripción es incorrecta. Debería ser @prescription = Prescription.new

3
  • Es correcto, ya lo corregí como tu indicas y en efecto me deja avanzar a la vista 'new' el problema ahora es que no me muestra la tabla con los botones Edit, Show, Delete. es como si no estuviese cargando el parcial del _form, el form lo hice así: <% form_for prescription_path(@prescription, @consultation) do |f| %> y me tira este error: No route matches {:action=>"show", :consultation_id=>"1", :controller=>"prescriptions", :id=>#<Prescription id: nil, prescription: nil, consultation_id: nil, created_at: nil, updated_at: nil>} missing required keys: [:id] el 18 ago. 2016 a las 17:40
  • @CarlosGómez hay veces en que siendo mas explícito en el ingreso de parámetros resulta. Prueba con prescription_path(id: @prescription, consultation_id: @consultation) o con prescription_path(id: @prescription.id, consultation_id: @consultation.id). Si ninguna resulta, ejecuta rake routes para comparar que la ruta generada por rails es la que efectivamente estas usando. el 20 ago. 2016 a las 3:51
  • @CarlosGómez si la respuesta te fue útil, puedes marcarla como aceptada. Saludos el 22 ago. 2016 a las 20:37

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.