Forum: Ruby on Rails Passing parameters and using associations

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.
22e571c8725e328d21e9ac38aeab9916?d=identicon&s=25 Michael Palmer (mariachi77)
on 2005-11-30 11:20
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-11-30 13:15
(Received via mailing list)
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

<form action="create_page" method="post">
<%= text_area "article_page", "text", :cols => 50, :rows => 20 %>
<%= hidden_field "article", "id" %>
<input type="submit" value="Save" />
</form>

gets compiled to:

<form action="create_page" method="post">
<textarea cols="50" rows="20" id="article_page_text"
name="article_page[text]"><%= @article_page.text %></textarea>
<input type="hidden" id="article_id" name="article[id]" value="<%= @
article.id %>" />
<input type="submit" value="Save" />
</form>

see the below documentation on how form helpers work

http://rails.rubyonrails.com/classes/ActionView/He...
This topic is locked and can not be replied to.