Forum: Ruby on Rails Foreign keys

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ambrose (Guest)
on 2007-01-16 23:49
(Received via mailing list)
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:

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

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

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

    <%= 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
-----------------------------
Ambrose (Guest)
on 2007-01-18 02:56
(Received via mailing list)
anyone have a moment - this must be so simple!
This topic is locked and can not be replied to.