Guest User

Untitled

a guest
Sep 14th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. Object in hidden_field_tag becomes nil when partial is called into an index (yet, works in individual Show view)
  2. <% content_for(:scripts) do %>
  3. <%= javascript_include_tag 'rating_ballot'%>
  4. <% end %>
  5.  
  6. <div id="talesFeedHome">
  7. <p class="notice"><%= notice %></p>
  8. <%= render @tales.sort_by { |tale| tale.created_at }.reverse %>
  9. </div>
  10.  
  11. <p class="clear">&nbsp;</p>
  12.  
  13. <% if signed_in? %>
  14.  
  15. <div id="homeTales">
  16. <ul>
  17. <div id="taleShow">
  18. <div id="controlPanel">
  19. <li id="taleUserName"><%= tale.user.name %></li>
  20. <li id="averageRating"> Your Rating:<br /><%= render "tales/stars" %></li>
  21. </div>
  22. <div id="taleDisplay">
  23. <li><%= link_to(tale) do %>
  24. <span><%= tale.title %> </span>
  25. <span><%= tale.content %></span>
  26. <% end %>
  27. </li> <br />
  28. </div>
  29. </div>
  30. </ul>
  31. </div>
  32.  
  33. <% else %>
  34. ...
  35.  
  36. <div id="starRating">
  37. <%= form_for(rating_ballot, :remote => true, :html => { :class => 'rating_ballot' }) do |f| %>
  38. <%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
  39. <%= radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>
  40. <%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
  41. <%= radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>
  42. <%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
  43. <%= radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>
  44. <%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
  45. <%= radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>
  46. <%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
  47. <%= radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>
  48.  
  49. <%= hidden_field_tag(:tale_id, @tale.id) %>
  50. <% end %>
  51. </div>
  52.  
  53. Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
  54.  
  55. class PagesController < ApplicationController
  56. def home
  57. @tales = Tale.all
  58. end
  59. end
  60.  
  61. class TalesController < ApplicationController
  62. respond_to :html, :js
  63.  
  64. def new
  65. @tale = Tale.new
  66. end
  67.  
  68. def show
  69. @tale = Tale.find(params[:id])
  70. end
  71.  
  72. def index
  73. @tales = Tale.all
  74. @tale = Tale.find(params[:id])
  75. end
  76. ...
  77.  
  78. class RatingsController < ApplicationController
  79. before_filter :authenticate_user!
  80. respond_to :html, :js
  81.  
  82. def create
  83. @tale = Tale.find_by_id(params[:tale_id])
  84. @rating = Rating.new(params[:rating])
  85. @rating.tale_id = @tale.id
  86. @rating.user_id = current_user.id
  87.  
  88. if @rating.save
  89. respond_to do |format|
  90. format.html { redirect_to @tale, :notice => "Your rating has been saved" }
  91. format.js
  92. end
  93. end
  94. end
  95.  
  96. def update
  97. @rating = current_user.ratings.find_by_tale_id(params[:tale_id])
  98. @tale = @rating.tale
  99.  
  100.  
  101. if @tale and @rating.update_attributes(params[:rating])
  102. respond_to do |format|
  103. format.html { redirect_to @tale, :notice => "Your rating has been updated" }
  104. format.js
  105. end
  106. end
  107. end
  108. end
Add Comment
Please, Sign In to add comment