如何创建在rails 3中创建关注者的路径?

时间:2011-04-26 20:37:30

标签: ruby-on-rails routes

我正在使用acts_as_followers并希望知道我可以传递给link_to的路径:remote =>真正的链接,以便用户可以跟随各种不同的实体。

这是我在路线中所拥有的(通过耙路线)

  

遵循
  /users/follow/:followed_type/:followed_id(.:format)   {:控制器=> “中的用户”,   :动作=> “中follow_this”}

这就是routes.rb中的内容:

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

但是我不清楚如何使用路径助手将值传递给URL字符串?我需要将类型作为字符串传递(例如“Vendor”),并且: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

1 个答案:

答案 0 :(得分: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 < / p>