agonzalo
agonzalo

Reputation: 15

Rails: How to acces @elements from new.html.erb

and thanks for Help!!

I´m Building my fisrt Rails app, and have this problem:

I made an Scaffold for a Class called "Expediente", from the index.html.erb I can gain acces over @expedientes but I cant do it from new.html.erb.

Here´s the code:

//index.html.erb, this run great: . . .

    <% @expedientes.each do |expediente| %>

    <tr>
    <td class="custom"> <%= expediente.id %> </td>

    <td class="custom"> <%= expediente.tipo_expediente %> </td>

    <td class="custom">

<% expediente.pacientes.each do |paciente| %>

    <%= paciente.nombre_completo %><br/>
<% end %>

. . .

//new.html.erb, this doesnt works at all and sends undefined method `each' for nil:NilClass error message:

. . .

      <% @expedientes.each do |expediente| %>

      <%if expediente.individual?%>

      <% expediente.pacientes.each do |paciente| %>

     <option value=126><%= paciente.nombre_completo %></option>

     <%end%>

     <%end%>  

     <%end%>

. . .

Again thanks fot your Help!!

Upvotes: 1

Views: 117

Answers (3)

Ross
Ross

Reputation: 1562

To combine the above 2 answers In you index method you might have something like

@expedientes = Expediente.all 

in New method you would have

@expedient = Expediente.new

add this in your new method

@expedientes = Expediente.all  

final

def new 
    @expedient = Expediente.new
   @expedientes = Expediente.all  
end

Upvotes: 0

sevenseacat
sevenseacat

Reputation: 25039

Because the index action in your ExpedientesController (which renders the index.html.erb view) defines the variable @expedientes, but your new action (which renders the new.html.erb view) does not.

Upvotes: 2

kries
kries

Reputation: 199

Your New action in the controller builds a single instance of your Expedientes class, therefore, it is not a collection you can iterate over with each.

Upvotes: 0

Related Questions