2012-01-31 43 views
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是這樣的:

http://localhost:3000/?remote=true&update=total_value_29&url%5Baction%5D=vote_up&url%5Bcontroller%5D=votes&url%5Bid%5D=29

該行動是在一個名爲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 

我不知道這是否是正常或我有一個語法錯誤的地方?

回答

1

link_to的選項只是不喜歡看的是,軌道認爲你是給所有的選項都是它的路由選擇

link_to 'link text', {:controller => ..., :action => ...}, {:remote => true, ...} 

是更喜歡它。這是不應該被用於構建URL應該是在第二散列東西(有很多的例子在api docs

相關問題