arwed
(arwed)
October 11, 2009, 10:35pm
1
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| %>
<li><% rating.category.name %> <% rating_form.text_field :rating
%></li>
<% end %>
<% end %>
...
Unfortunately this doesn't work.
Any help would great.
Samiron
(Samiron)
October 12, 2009, 8:55am
2
...
<% if @hotel.categories.any ? %>
<% @hotel.ratings.each do |rating| %>
<% fields_for @hotel.ratings do |rating_form| %>
<li><% rating.category.name %> <% rating_form.text_field :rating
%></li>
<% 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
arwed
(arwed)
October 12, 2009, 12:51pm
3
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:
<h2>Hotel Ratings:</h2>
<% @hotel.ratings.each do |show| %>
<li><%= 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
Regards
Arwed