0
我有這個在我的意見之一:單擊Rails link_to方法後,所有選項都顯示在URL欄中是否正常?
<%= link_to "Vote up", :url => {:controller => :votes, :action => :vote_up, :id => i.id},
:update => "total_value_#{i.id}",
:remote => true %>
<%= link_to "Vote down", :url => {:controller => :votes, :action => :vote_down, :id => i.id},
:update => "total_value_#{i.id}",
:remote => true %>
當我點擊說,投票了,我得到的URL是這樣的:
該行動是在一個名爲votes_controller.rb控制器:
class VotesController < ApplicationController
def vote_up
check = Votes.find(:first,
:conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]])
post = Post.find(params[:id])
if check.nil?
vote = Votes.new
vote.post_id = params[:id]
vote.user_id = session[:user_id]
vote.value = true
vote.save
post.total_value += 1
post.save
render :text => post.total_value
elsif check.value == false
check.value = true
check.save
post.total_value += 2
post.save
render :text => post.total_value
else
render :text => "You have already voted up for this post."
end
end
def vote_down
check = Vote.find(:first,
:conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]])
post = Post.find(params[:id])
if check.nil?
vote = Vote.new
vote.post_id = params[:id]
vote.user_id = session[:user_id]
vote.value = true
vote.save
post.total_value -= 1
post.save
render :text => post.total_value
elsif check.value == true
check.value = false
check.save
post.total_value -= 2
post.save
render :text => post.total_value
else
render :text => "You have already voted down for this post."
end
end
end
我不知道這是否是正常或我有一個語法錯誤的地方?