[Ror-es] show and update problem with relation

Fran Hurtado
Tue May 17 08:27:48 GMT 2011


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*
/

<%= .name %> <%= .surname %>

DNI: <%= .dni %>
Email :<%= .email %>
Phone:<%= .phone %>
City:<%= .city %>
State:<%= .provincia.name %>

<%= 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* /

Changing your data


<% form_tag :action => 'update', :id => do %> <%= hidden_field 'candidate', 'candidateid', :value => @userid %>
:<%= text_field 'candidate', 'name' %>
:<%= text_field 'candidate', 'surname' %>
:<%= text_field 'candidate', 'email' %>
:<%= text_field 'candidate', 'phone' %>
:<%= text_field 'candidate', 'city' %>
:<%= collection_select(:candidate,:,:id,:name) %>

<%= submit_tag "Update" %> <% end %>/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.simplelogica.net/pipermail/ror-es/attachments/20110517/9452ccf8/attachment-0001.htm