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/Helpers/FormHelper.html#M000338
On 11/30/05, Michael Palmer <mike-b3K1ntNyKhlBDgjK7y7TUQ@public.gmane.org>
wrote:>
> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails