2011-04-26 29 views
0

我正在使用acts_as_followers,並想知道我可以傳遞給link_to:remote => true鏈接的路徑,以便用戶可以關注各種不同的實體。如何創建一個路徑在軌道3中創建追隨者?

以下是我在路由(通過耙路由)

遵循
/users/follow/:followed_type/:followed_id(.:format) {:控制器=> 「用戶」 , :動作=> 「follow_this」}

這是在routes.rb中:

match 'users/follow/:followed_type/:followed_id' => 'users#follow_this', :as => "follow" 

但我不清楚如何使用路徑助手將值傳遞到URL字符串?我需要將字符串作爲字符串(例如「供應商」)和:id ...但我該怎麼做?

這是我可以讓用戶按下鏈接,它會調用此操作並創建以下關係。

46 def follows_this 
47 
48  followed_type = params[:followed_type] 
49  followed_class = class_type.camelize.constantize 
50  followed = followed_class.find(params[:followed_id]) 
51  current_user.follow(followed) #uses acts_as_follower plugin 
52 
53 end 

回答

2
put '/users/follow/:followed_type/:followed_id' => 'users#follow_this', :as => "follow" 

使用

<%= link_to "Follow White Rabbit", follow_path(:followed_type => "some type", :followed_id => "some_id"), :method => :put %> 

match意味着你可以調用任何請求:GET,POST,PUT或DELETE。所以最好指定一個你想使用的。至於你更新一些數據 - 使用PUT,如果你正在創建一些數據 - POST,如果你刪除 - DELETE,如果你只是取 - GET

+0

我看....我沒有使用get - - 這是做什麼,讓我試試! – Angela 2011-04-26 20:43:40

+0

我用'匹配'...嗯......兩者有什麼區別? – Angela 2011-04-26 20:44:20

+0

@floor - 我得到了一個錯誤:(沒有路由匹配{:follow_type =>「Vendor」,:follow_id => 1,:action =>「follow_this」,:controller =>「users」} – Angela 2011-04-26 20:46:08