Passing parameters and using associations

Hi, I’m a PHP programmer doing my first post-Agile rails project and I
am having major issues understanding how to handle associations and
utilize GET/POST params in rails.

The particulars are as follows:

The site consists of Articles. Each article may have many pages.

Each page has an corresponding article_id which assigns it to an
article.

Now my problems arise when I try to pass that article_id into the “Add
new page” form. I (assume I) need that article_id to be part of the form
so that it will be passed to the create action. I would also like to use
that article_id to access that article’s information so I can display
the Article’s title for the user.

I currently have the new_page (form) and create_page actions in the
Article controller. Perhaps this is a structural problem?

Models:

class Article < ActiveRecord::Base
has_many :article_pages
end

class ArticlePage < ActiveRecord::Base
belongs_to :article
validates_associated :article
end

Article Controller (edited):

class ArticlesController < ApplicationController

Article actions removed for

Article Page actions

def new_page
#@session[:article_id] = @params[:article_id]
#@article_page = ArticlePage.new
#@article = Article.find(params[:article_id]
@article_page = ArticlePage.new
# how do I pass that article_id parameter into the form?
end

def create_page
@article = Article.find params[:article_id]
@article.article_pages.create params[:text]
flash[:notice] = ‘Page was successfully added to this article.’
render :action => ‘show’
end

end

I would appreciate any help, advice, guidance on this subject as to the
approach to take when faced with this situation.

Cheers,

Mike

def new_page
# create an instance of article to use in the form
@article = Article.find(params[:article_id])
# create an instance of article_page to use in the form
@article_page = ArticlePage.new
end

def create_page
@article = Article.find(params[:article][:id])
@article.article_pages.create(params[:article_page])
flash[:notice] = ‘Page was successfully added to this article.’
render(:action => :show)
end

view: new_page.rhtml

<%= text_area "article_page", "text", :cols => 50, :rows => 20 %> <%= hidden_field "article", "id" %>

gets compiled to:

<%= @article_page.text %>

see the below documentation on how form helpers work

http://rails.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000338