我有一个竞赛模型的展示视图,可以显示2张随机照片:
<% if @contest.photos.size < 2 %>
<p>not enough photos in this contest</p>
<% else %>
<%= link_to (image_tag @random_photo1.image.url(:thumb)), upvote_path(id: @contest.id, photo_id: @random_photo1.id) %>
<%= @random_photo1.score %>
<%= link_to (image_tag @random_photo2.image.url(:thumb)), "#" %>
<%= @random_photo2.score %>
<% end %>
这是我的表演行动:
def show
@contest = Contest.find(params[:id])
@user = current_user
random_two_photos = @contest.photos.limit(2).order("RANDOM()")
@random_photo1 = random_two_photos[0]
@random_photo2 = random_two_photos[1]
end
这里是我的两个行动路线:
get 'contest/:id/photo/:photo_id' => 'contests#enter_contest', as: :enter_contest
get 'contest/:id/photo/:photo_id' => 'contests#upvote', as: :upvote
我如何撰写我的&#34; upvote&#34;控制器动作,以便我点击的照片获得100分,而另一个获得-100分?我正在使用postgresql。
答案 0 :(得分:1)
我会使用其他路线:
get 'contests/:id/start' => 'contests#start', as: start_contest
put 'contests/:id/vote' => 'contests#vote', as: vote
然后,在 contests_controller / start 操作
中def start
@contest = Contest.find(params[:id])
random_photos = @contest.photos.limit(2).order("RANDOM()")
@photo1 = random_photos.first
@photo2 = random_photos.last
end
在 contests_controller / start 操作
中def vote
@contest = Contest.find(params[:id])
@photo1 = Photo.find(params[:up_photo_id])
@photo2 = Photo.find(params[:down_photo_id])
@photo1.upvote # you need to put this method in Photo model
@photo2.downvote # you need to put this method in the Photo model
end
在竞赛/开始视图中
<%= link_to image_tag(@photo1.image.url(:thumb)), vote_path(id: @contest.id, up_photo_id: @photo1.id, down_photo_id: @photo2.id) %>
和...
<%= link_to image_tag(@photo2.image.url(:thumb)), vote_path(id: @contest.id, up_photo_id: @photo2.id, down_photo_id: @photo1.id) %>
请记住:您需要将选项
method: :put
传递给link_to帮助程序才能执行PUT而不是GET。
<强>改进强>
vote_link(contest, up_photo, down_photo)
以避免代码重复。photos.limit(2).order("RANDOM()")
移动到模型中的方法中。类似于:@contest.random_photos(2)
我写的代码没有经过测试。但是你明白了......
更新使用单一展示行动
您需要定义单个路线
resources :contests, only: [:show]
然后在 contests_controller / show 操作
def show
@contest = Contest.find(params[:id])
if params.has_key? :up_photo_id && params.has_key? :down_photo_id
@photo1 = Photo.find(params[:up_photo_id])
@photo2 = Photo.find(params[:down_photo_id])
@photo1.upvote # you need to put this method in Photo model
@photo2.downvote # you need to put this method in the Photo model
else
random_photos = @contest.photos.limit(2).order("RANDOM()")
@photo1 = random_photos.first
@photo2 = random_photos.last
end
end
在 show.html.erb
中<%= link_to image_tag(@photo1.image.url(:thumb)), contest_path(id: @contest.id, up_photo_id: @photo1.id, down_photo_id: @photo2.id) %>
和...
<%= link_to image_tag(@photo2.image.url(:thumb)), contest_path(id: @contest.id, up_photo_id: @photo2.id, down_photo_id: @photo1.id) %>
更新:获取不同的照片
class Contest < ActiveRecord::Base
def random_photos
self.photos.order("RANDOM()")
end
def two_different_photos
photo1 = random_photos.first
raise 'no photos' unless photo1 # raise an exception or something
photo2 = random_photos.where.not(id: photo1.id).first
raise 'only one photo' unless photo2 # raise an exception or something
[photo1, photo2]
end
end
然后您可以在控制器中使用@contest.two_different_photos
...