Edit HMT with fields_for

Hi,
I have 3 models:

hotel
has_many :ratings
has_many :categories, :through => :ratings

category
has_many :ratings
has_many :hotels, :through => :ratings

rating
belongs_to :hotel
belongs_to :category

I have categories like food, rooms etc. that I want to rate in my
hotel edit view:

<% if @hotel.categories.any? %>
<% @hotel.ratings.each do |rating| %>
<% fields_for @hotel.ratings do |rating_form| %>

  • <% rating.category.name %> <% rating_form.text_field :rating
    %>

  • <% end %>
    <% end %>

    Unfortunately this doesn’t work.

    Any help would great.


    <% if @hotel.categories.any? %>
    <% @hotel.ratings.each do |rating| %>
    <% fields_for @hotel.ratings do |rating_form| %>

  • <% rating.category.name %> <% rating_form.text_field :rating
    %>

  • <% end %>
    <% end %>

    As you told you are trying to edit hotel information. So I think you
    should have like following
    <% form_for(@hotel) do |form| %>
    <% end %>

    and inside this you might use like
    <% form.fields_for :ratings do |rating_field| %>
    # print rating fields
    <% end %>

    If the above doesnt help you, would you please provide more insight
    about what and how you are actually want to print ratings in hotel
    edit form?

    Thank you.

    Samiron paul

    http://www.code71.com

    Hi Paul,

    thanks for your reply.

    In my categories table I have some records: rooms, food, service etc.

    my show.html.erb looks like this:

    Hotel Ratings:

    <% @hotel.ratings.each do |show| %>
  • <%= show.category.name %> <%= show.rating %> <% end %>

    For a hotel this gives me:

    Hotel Ratings:

    rooms 3
    food 4
    service 3

    This part works fine. Now I would like to edit the Ratings.

    In my hotel edit form I want to show each one of these categories with
    a text_field behind it, so I can enter a rating.

    <% form_for(@hotel) do |form| %>

    <% form.fields_for :ratings do |rating_field| %>

    <%= show me the hotel-rating-category names %> <%= rating_field
    

    (s).text_field :rating %>

    <% end %>

    <% end %>

    How do I loop through all my categories, show their names and enter a
    value for each different category?

    I hope this describes my problem a little better.

    Probably I’m thinking much too complicated :frowning:

    Regards
    Arwed