Skip to content

Instantly share code, notes, and snippets.

@typesend
Forked from starflyer59/gist:2505474
Created April 28, 2012 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typesend/2520726 to your computer and use it in GitHub Desktop.
Save typesend/2520726 to your computer and use it in GitHub Desktop.
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
######### _stars.html.erb ###################
<div id="starRating">
<%= form_for(rating_ballot, :html => { :class => 'rating_ballot' }) do |f| %>
<%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
<%= radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>
<%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
<%= radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>
<%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
<%= radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>
<%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
<%= radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>
<%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
<%= radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>
<%= hidden_field_tag(:fable_id, @fable.id) %>
<%= f.submit :Submit %>
<% end %>
</div>
###### ratings_controller.rb ######################
class RatingsController < ApplicationController
before_filter :authenticate_user!
def create
@rating = Rating.new(params[:rating])
@rating.fable = Fable.find(params[:fable_id]) # Tip: find(21) is equivalent to find_by_id(21)
@rating.user = current_user
# Tip: Your code depends on the Rating having a fable,
# so you should have at least some code that tests to make sure it was found.
# *** Maybe this is what the nil error is about? ***
if @rating.fable and @rating.save
respond_to do |format|
format.html { redirect_to fable_path(@fable), :notice => "Your rating has been saved" }
format.js
end
end
end
def update
# Tidied this up a bit
@rating = current_user.ratings.find_by_fable_id(params[:fable_id])
@fable = @rating.fable
if @fable and @rating.update_attributes(params[:rating])
respond_to do |format|
format.html { redirect_to fable_path(@fable), :notice => "Your rating has been updated" }
format.js
end
end
end
end
####### _fable.html.erb #########
<% if signed_in? && fable.user == current_user %>
<div id="homeFables">
<ul>
<div id="fableShow" class="rounded-corners-mild">
<div id="controlPanel" class="gravatar">
<span> <%= link_to gravatar_for(fable.user, :size => '90'), fable.user %></span>
<li id="fableUserName"><%= fable.user.name %></li>
<li id="twitterFable"><%= link_to "@#{fable.user.twitter}", "http://twitter.com/#{fable.user.twitter}" %></li>
<li id="fableEdit"><%= link_to "edit fable", "/editor" + "/fables" + "/#{fable.id}", id: "edit_link", data: {save_url: mercury_update_fable_path(fable)} %></li>
<li> <%= render 'fables/stars' %> </li>
</div>
<div id="fableCore">
<li><%= link_to(fable) do %>
<span class="fableTitle"><%= fable.title %> </span>
<span id="fableContent" class="mercury-region" data-type="editable"><%= raw fable.content %></span><br />
<span style="font-size:65%; float:right;"><%= fable.created_at %> </span><br />
<% end %>
</li> <br />
</div>
</div>
</ul>
</div>
<% else %>
<div id="homeFables">
<ul>
<div id="fableShow" class="rounded-corners-mild">
<div id="controlPanel" class="gravatar">
<span> <%= link_to gravatar_for(fable.user, :size => '90'), fable.user %></span>
<li id="fableUserName"><%= fable.user.name %></li>
<li id="twitterFable"><%= link_to "@#{fable.user.twitter}", "http://twitter.com/#{fable.user.twitter}" %></li>
<li><%= render 'fables/stars' %></li>
</div>
<div id="fableCore">
<li><%= link_to(fable) do %>
<span class="fableTitle"><%= fable.title %> </span>
<span class="fableContent"><%= raw fable.content %></span><br />
<span style="font-size:65%; float:right;"><%= fable.created_at %></span><br />
<% end %>
</li> <br />
</div>
</div>
</ul>
</div>
<% end %>
@typesend
Copy link
Author

Is the nil error being thrown on line 18 of your template? (Whatever line corresponds to line 18 in this gist, of course.)

@starflyer59
Copy link

Yes. Line 18

@typesend
Copy link
Author

Try putting <% raise @fable %> on line 17 and see if @fable actually has stuff in it

@typesend
Copy link
Author

Sorry, I meant <% raise @fable.to_yaml %> which should be better :)

@starflyer59
Copy link

I mean this:

Showing /Users/Daniel/Sites/fables2/app/views/fables/_stars.html.erb where line #13 raised:

@starflyer59
Copy link

Dang. Gist keeps turning my 3 dashed into a line. "- - -"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment