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

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?


class Article < ActiveRecord::Base
has_many :article_pages

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

Article Controller (edited):

class ArticlesController < ApplicationController

Article actions removed for

Article Page actions

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

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’


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



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 =

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

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