Rails questions

Discussion in 'Ruby' started by Joe Van Dyk, Dec 13, 2004.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    Spent some time this weekend working on a Rails application. It's great
    stuff, coming from a php/mysql "hack it together" background. But I have a
    couple questions...

    1)
    I have a controller called "houses". In that, there's an action called
    "modify". modify's view (modify.rhtml) has a form with an action
    "save_house". When the user hits submit, modify gets called with id
    save_house (i.e. /houses/modify/save_house). What I want to have happen is
    the action "save_house" called. I have another action "new", and new.rhtml
    has a form with action "create_house", and when the user hits submit, the
    action "create_house" is called, which is what I want to have happen. I'm
    not sure why that's not the case with modify.

    2)
    has_and_belongs_to_many question: I have a model "House" that
    has_and_belongs_to_many "Amenity". Is there a "Best Way(tm)" to display
    this relationship in the controller/view? The way it works currently:

    houses_controller#new:
    @amenities=Amenities.find_all

    new.rhtml:
    (loops through @amenities, spits out checkboxes.)

    house_controller#create:
    @house = House.new(@params["house"].save
    @house.amenities.clear
    @params["amenities"].each do |amenity|
    # @params["amenities"] is an array of amenity id's that the user
    selected
    temp_amenity = Amenities.new
    temp_amenity.id = amenity
    @house.amenities << amenity
    end

    This seems pretty clumsy to me.


    Thanks for any input!

    Joe
    Joe Van Dyk, Dec 13, 2004
    #1
    1. Advertising

  2. > 1)
    > I have a controller called "houses". In that, there's an action called
    > "modify". modify's view (modify.rhtml) has a form with an action
    > "save_house". When the user hits submit, modify gets called with id
    > save_house (i.e. /houses/modify/save_house). What I want to have
    > happen is
    > the action "save_house" called. I have another action "new", and
    > new.rhtml
    > has a form with action "create_house", and when the user hits submit,
    > the
    > action "create_house" is called, which is what I want to have happen.
    > I'm
    > not sure why that's not the case with modify.


    It sounds like you're using scaffolding, which had a problem regarding
    this in Rails 0.8.5. You can fix it manually by doing something like:

    <%= form("house", :action => url_for:)action => "save_house")) %>

    ...if my guess at what your problem is have any grounding in reality.
    This problem is fixed in the very forthcoming (and massive) Rails 0.9
    upgrade.

    > 2)
    > has_and_belongs_to_many question: I have a model "House" that
    > has_and_belongs_to_many "Amenity". Is there a "Best Way(tm)" to
    > display
    > this relationship in the controller/view? The way it works currently:
    >
    > house_controller#create:


    @house = House.new(@params["house"].save
    @house.amenities << Amenities.find(@params["amenities"])

    Amenities.find accepts and array and will return a such.
    @house.amenities.<< does the same and creates a link between the house
    and amenity for each of the records found.

    For followup questions, please consider signing up for the mailing
    list[1] or get on IRC[2] for real-time help.

    Thanks for enjoying Rails!

    [1] http://lists.rubyonrails.org/mailman/listinfo/rails
    [2] http://www.rubyonrails.org/show/IRC
    --
    David Heinemeier Hansson,
    http://www.basecamphq.com/ -- Web-based Project Management
    http://www.rubyonrails.org/ -- Web-application framework for Ruby
    http://macromates.com/ -- TextMate: Code and markup editor (OS X)
    http://www.loudthinking.com/ -- Broadcasting Brain
    David Heinemeier Hansson, Dec 13, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. D.
    Replies:
    9
    Views:
    168
    Jamis Buck
    Nov 4, 2004
  2. Replies:
    6
    Views:
    181
    Michael Campbell
    Mar 17, 2005
  3. Greg McIntyre

    Rails + Amrita, Rails + XTemplate?

    Greg McIntyre, Apr 28, 2005, in forum: Ruby
    Replies:
    2
    Views:
    125
    Greg McIntyre
    Apr 28, 2005
  4. Michael Schuerig
    Replies:
    3
    Views:
    221
    Carl Youngblood
    Jun 17, 2005
  5. Brian Takita
    Replies:
    1
    Views:
    278
    Zed A. Shaw
    Sep 5, 2005
Loading...

Share This Page