Skip to content

Instantly share code, notes, and snippets.

@starflyer59
Created April 27, 2012 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save starflyer59/2505474 to your computer and use it in GitHub Desktop.
Save starflyer59/2505474 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
@fable = Fable.find_by_id(params[:fable_id])
@rating = Rating.new(params[:rating])
@rating.fable_id = @fable.id
@rating.user_id = current_user.id
if @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
@fable = Fable.find_by_id(params[:fable_id])
@rating = current_user.ratings.find_by_fable_id(@fable.id)
if @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 %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment