Foreign keys

Ambrose
Tue, 16 Jan 2007 20:21:15 -0000

I have a building a very simple app with which to learn Rails where a
user can create as many CVs (resumes) as they wish.

For now I have:

ActiveRecord::Schema.define(:version => 4) do

  create_table "cvs", :force => true do |t|
    t.column "uni", :string
    t.column "class", :string
    t.column "degree", :string
    t.column "user_id", :integer
  end

  create_table "users", :force => true do |t|
    t.column "first_name", :string
    t.column "last_name", :string
  end

end

In the ''cv'' model I have:

class Cv < ActiveRecord::Base
belongs_to :user
end

in the ''user'' model I have:

class User < ActiveRecord::Base
has_many :cvs
end

I''ve added my controller code at the end of the message in case that
helps.

When I come to code the view for creating a new cv I want the new CV
that is created to have the ''user_id'' as the foreign key. But
I
just cannot work out what code to write.

I have been trying even to get the user''s name to appear in the
createcv view but failed:

<%= params[:action].capitalize %> CV

This is <%= @user.user.first_name %>

<%= start_form_tag :action => ''create'' %>
Enter your degree title
    <%= text_field (''cv'', ''degree'')
%>
Enter expected/achieved class
    <%= text_field (''cv'', ''class'')
%>
Enter institution name
    <%= text_field (''cv'', ''uni'')
%>

    <%= submit_tag ''Save'' %>

<%= end_form_tag %>

I guess I am just confused by routing.

I know this is incredibly simple and yet I have been through the 4
books I have, all of them on Ruby and Rails ;-), various online
tutorials and yet somehow I just cannot work out how to do this.

If someone could explain it to my like I was 5 then maybe I''d get it.

Thanks

Ambrose


cv-controller
-----------------

class CvController < ApplicationController

  def create
    @cv = Cv.new(params[:cv])
    if(params[:cv])
      if @cv.save
        flash[:notice] = ''You saved a new CV.''
        redirect_to :action => ''view'', :id => @cv
      end
    else
    # show form
    render :action => ''create''
    end
  end

  def view
    @cv = Cv.find(params[:id])
  end


  def edit
  end

  def delete
  end
end
-----------------------


user_controller
----------------------
class UserController < ApplicationController

  def create
    @user = User.new(params[:user])
    if(params[:user])
      if @user.save
        flash[:notice] = ''You saved a new user.''
        redirect_to :action => ''view'', :id => @user
      end
    else
    # show form
    render :action => ''create''
    end
  end

  def view
    @user = User.find(params[:id])
  end

  def edit
  end

  def index
    list
    render :action => ''list''
  end

    def list
    @user_pages, @users = paginate :users, :per_page => 10
  end

end
-----------------------------


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---