[Ror-es] show and update problem with relation

Manuel González Noriega
Tue May 17 10:32:57 GMT 2011


Hola Fran,

me queda la duda de a qué lista querías mandar el mensaje. Si es a la
lista en español, aparte de que puedes obviamente escribir en español,
por favor, usa la dirección 

Tu código si tiene problemas evidentes, los discutimos en la otra lista.

On 17 May 2011 10:27, Fran Hurtado  wrote:
> Hello!
>
> At first sorry for my English!
>
> I am having problem with a show and update view. I have two tables in my
> database named: "candidates" and "provincias". Candidates has a int column
> named 'provincia' which is related with int column 'id' on the table
> "provincias".
>
> I am using nifty:generators for authentication, and the 'id' of the user is
> related with Candidates int column named 'candidateid'.
>
> I have the two models:
>
> Model candidate.rb
> class Candidate < ActiveRecord::Base
>     belongs_to :provincia
> end
>
> Model provincia.rb
> class Provincia < ActiveRecord::Base
>     has_many :candidates
> end
>
> And my controller:
>
> Controller candidates.controller.rb
> class CandidatesController < ApplicationController
>
>    def list
>        = Candidate.find(:all)
>    end
>
>    def show
>      = Candidate.find_by_candidateid(params[:id])
>     if .blank?
>         redirect_to :controller => "candidates", :action => "new"
>     end
>    end
>
>    def new
>        = Candidate.new
>       @userid = session[:user_id]
>        = Provincia.find(:all)
>    end
>
>    def create
>        = Candidate.new(params[:candidate])
>       if .save
>             redirect_to :action => 'show'
>       else
>             render :action => 'new'
>       end
>    end
>
>    def edit
>         = Candidate.find(params[:id])
>        @userid = session[:user_id]
>         = Provincia.find(:all)
>    end
>
>    def update
>         = Candidate.find(params[:id])
>     if .update_attributes(params[:candidate])
>         redirect_to :action => 'show', :id => 
>     else
>         render :action => 'edit'
>     end
>    end
>
>    def show_provincias
>        = Provincia.find(params[:id])
>    end
>
> end
>
>
> When I try to see the show view I have this error: undefined method `name'
> for nil:NilClass
>
> View show.html.erb
> <h1><%= .name %> <%= .surname %></h1>
> <table cellpadding="4" cellspacing="0">
>     <tr><td><strong>DNI: </strong></td><td><%= .dni %></td></tr>
>     <tr><td><strong>Email :</strong></td><td><%= .email
> %></td></tr>
>     <tr><td><strong>Phone:</strong></td><td><%= .phone
> %></td></tr>
>     <tr><td><strong>City:</strong></td><td><%= .city %></td></tr>
>     <tr><td><strong>State:</strong></td><td><%= .provincia.name
> %></td></tr>
> </table><br />
>
> <%= link_to 'Edit', {:action => 'edit', :id => session[:user_id]} %>
>
> When I try to update my candidate info I have this error:
> Provincia(#1817717145020) expected, got String(#1817757682680)
>
> View edit.html.erb
> <p>Changing your data</p><br />
>
> <% form_tag :action => 'update', :id =>  do %>
>     <table cellpadding="4" cellspacing="0">
>     <tr><td><label for="candidate_name">Name</label>:</td><td><%= text_field
> 'candidate', 'name' %></td></tr>
>     <tr><td><label for="candidate_surname">Surname</label>:</td><td><%=
> text_field 'candidate', 'surname' %></td></tr>
>     <tr><td><label for="candidate_email">Email</label>:</td><td><%=
> text_field 'candidate', 'email' %></td></tr>
>     <tr><td><label for="candidate_phone">Phone</label>:</td><td><%=
> text_field 'candidate', 'phone' %></td></tr>
>     <tr><td><label for="candidate_city">City</label>:</td><td><%= text_field
> 'candidate', 'city' %></td></tr>
>     <tr><td><label for="candidate_provincia">State</label>:</td><td><%=
> collection_select(:candidate,:,:id,:name) %></td></tr>
>
>     <%= hidden_field 'candidate', 'candidateid', :value => @userid %>
>     </table><br />
>     <%= submit_tag "Update" %>
> <% end  %>
>
>
> _______________________________________________
> Proudly free of Ruby Forum crossposting since 01/07/2009
> Ror-es mailing list
> 
> 
>
>