0

undefined local variable or method在视图文件夹中收到此错误 toggle_follow_path'`。我可能对方法有问题,或者使用 off form_tag + toggle_follow_path 任何帮助都将受到欢迎,谢谢。顺便说一下,切换关注的目标是关注或取消关注某个人。

在路由文件中

 match '/:username/toggle_follow',       to: 'home#toggle_follow'

家庭控制器

def toggle_follow
     @user =  User.find_by_username(params[:username])
     if current_user.is_friend? @user
       flash[:notice] = "You are no longer following @#{@user.username}"
       current_user.remove_friend(@user)
     else
       flash[:notice] = "You are now following @#{@user.username}"
       current_user.add_friend(@user)
     end
     redirect_to user_flits_path(@user)

  end

看法

<h1><%= image_tag @user.gravatar_url, :align => "top" %> <%= @user.username %></h1>

<%= form_tag  toggle_follow_path, :method => :post do  %>
  <% if current_user.is_friend? @user %>
     <%=h submit_tag "Following"  , :class => "button" %>
  <% else %>
     <%=h submit_tag "Follow"  , :class => "button" %>
  <% end %>
<% end %>
<%=h render :partial => "flits_list", :locals => {:flits => @flits }%>
4

2 回答 2

1

使用:as选项match指定所需助手的名称:

match '/:username/toggle_follow', to: 'home#toggle_follow', as: 'toggle_follow'

这样,两者都toggle_follow_pathtoggle_follow_url被创建。

于 2012-12-05T18:27:41.507 回答
0

它需要接受一个:username参数。

试试这个:

toggle_follow_path(:username => "johndoe") # fill in the correct username.
于 2012-12-05T18:30:24.930 回答