Rails:未定义的方法`follow_user_path'

时间:2013-05-16 07:04:46

标签: ruby-on-rails-3.2

我第一次在我的rails应用程序上实现'跟随功能'。我有foodio视图和控制器。在这里,我希望注册用户关注美食。

我的食物控制员:

class FoodiosController < ApplicationController

  def show
    if params[:id].to_i.to_s == params[:id]
            @foodio = User.find(params[:id])
    else
            profile = Profile.find_by_brand_name(params[:id])
            if profile.nil?
              profile = Profile.find_by_first_name(params[:id])
            end
            @foodio = User.find(profile[:user_id])
    end
   @products = @foodio.products.paginate(:page => params[:page])
  end


  def follow
      @user = User.find(params[:id])
      current_user.follow(@user)
      respond_to do |format|
        format.js {render :action=>"follow"}
      end
    end

    def unfollow
      @user = User.find(params[:id])
      current_user.stop_following(@user)
      respond_to do |format|
        format.js {render :action=>"unfollow"}
      end
    end
  end

在foodio看来,我有:

show.html.erb:

            <div id="follow_user<%=@foodio.id%>">
              <% unless @foodio == current_user %>
                <% if current_user.following?(@foodio) %>
                  <%= link_to(unfollow_user_path(@foodio), :remote => true, :class => 'btn btn-primary') do %>
                  <i class="icon-remove icon-white"></i>
                  'Un-Follow'
                  <% end %>
                <% else %>
                  <%= link_to(follow_user_path(@foodio) ,:remote => true, :class => 'btn btn-primary') do %>
                  <i class="icon-ok icon-white"></i>
                  'Follow'
                  <%end%>
                <% end %>
              <% end %>
            </div>

_follow_user.html.erb

 <% unless @foodio == current_user %>
   <% if current_user.following?(@foodio) %>
     <%= link_to(unfollow_user_path(@foodio), :remote => true, :class => 'btn btn-primary') do %>
     <i class="icon-remove icon-white"></i>
     'Un-Follow'
     <% end %>
   <% else %>
     <%= link_to(follow_user_path(@foodio) ,:remote => true, :class => 'btn btn-primary') do %>
      <i class="icon-ok icon-white"></i>
       'Follow'
     <%end%>
   <% end %>
 <% end %>

follow.js.erb

$('#follow_user<%=@user.id%>').html('<%= escape_javascript(render :partial => "foodios/follow_user", :locals => {:user => @user}) %>');

unfollow.js.erb

$('#follow_user<%=@user.id%>').html('<%= escape_javascript(render :partial => "foodios/follow_user", :locals => {:user => @user}) %>');

并包含在用户模型中

acts_as_follower
acts_as_followable

在我的路线文件中,我添加了:

resources :foodios do
 member do
   get :follow
   get :unfollow
 end
end

但是我收到了错误:

  undefined method `follow_user_path on  <%= link_to(follow_user_path(@foodio) ,:remote => true, :class => 'btn btn-primary') do %> in show.html.erb

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您的路线不了解用户......他们只知道食物。 尝试:

follow_foodio_path(@foodio)