SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to pass default Param info in a form (add a post to a specific category)

    Hi.

    Regarding the Lenz Book:

    Let's say you have categories and stories.

    You have 3 categories: Music, Movies, and Books.

    Let's say you select the Music category. Now, you would like to post a story link inside the Music category.

    If you want to add a new story to a specific category, what code must you add to your story controller and story view template?

    I have searched and couldn't find much help. It must have something to do with params.

    My story controller contains:

    def new
    @story = Story.new(params[:story])
    @story.user = @session['user']

    if request.post? and @story.save

    @story.tag_with params[:tags] if params[:tags]

    flash['notice'] = 'your story has been accepted'
    redirect_to :action => 'index'
    end

    (I don't know if I must add filters to the controller to make sure that the specific category id is passed on in the form. )

    My story view is something like:

    <% form_for :story do |f| %>
    <p>
    title:<br />
    <%= f.text_field :title %>
    </p>
    <p>
    short description:<br />
    <%= f.text_field :description %>

    </p>
    <p>
    Tags:<br />
    <%= text_field_tag 'tags', params[:tags] %>
    </p>

    <%= submit_tag %>
    </p>
    <% end %>


    Thanks for any help!

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you could do that with params. You would pass the category id in a param in the link to this page, then store it in a hidden_field called category_id. That should do the trick.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •