0

Railsアプリに初めて「フォロー機能」を実装しています。私はfoodioビューとコントローラーを持っています。ここでは、サインアップしたユーザーに foodios をフォローしてもらいたいと考えています。

私の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

食通の見解では、私は持っています:

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

誰でもこれで私を助けることができますか?

4

1 に答える 1

0

あなたのルートはユーザーについて知りません...彼らはfoodiosについてしか知りません。試す:

follow_foodio_path(@foodio)
于 2013-05-16T07:08:53.447 に答える