Reputation: 59
there's an issue, that is bothering me. I'm following this "Ruby on Rails"-Tutorial to implement an ajaxified rating system http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3
Die author uses a self written helper method called "rating_ballot" which seems pretty redundant to me, because it checks if a rating has been given yet and otherwise forms a new one with
current_user.ratings.new
But that actually is being done more or less in the ratingscontroller
using this helper method the form looks like this
= form_for(rating_ballot, :html => { :class => 'rating_ballot' }) do |f|
But any other form (for example posting reviews) uses the instance variable instead of a helper method.
I want the form_for tag to look like this
= form_for(@rating, :html => { :class => 'rating_ballot' }) do |f|
but this only works for updating ratings, not creating new ones.
why is this "rating_ballot" so important ?
Upvotes: 0
Views: 162
Reputation: 4737
I'm guessing the value of @rating
is nil, which is why using this form for the #create
action isn't working. The first argument should be a new
object, or an object that represents an existing record, in order to create or update, respectively.
Another alternative way of using the form_for
method is to supply a symbol representing the name of the class and also specifying the :url
argument according to how your routes are specified. This is only good for creating though since the symbol doesn't represent an existing record.
Upvotes: 0
Reputation: 4264
Take a look on that part
def rating_ballot
if @rating = current_user.ratings.find_by_photo_id(params[:id])
@rating
else
current_user.ratings.new
end
end
It try to find out @rating if exists and create new instance if not exsits
you can do it in you controller for example:
@rating = current_user.ratings.find_by_photo_id(params[:id])
@rating ||= current_user.ratings.new # create instance if did not find existing once
ans then use it in form like you wrote
Upvotes: 0