为什么Rails在没有单击链接的情况下执行刷新操作

时间:2013-02-26 15:40:55

标签: ruby-on-rails ruby action link-to

我一直试图在网上到处找到这个答案而没有运气,也许这会帮助别人。

我安装了一个名为socialization的红宝石宝石,效果很好..它允许用户关注/取消关注/喜欢/不喜欢/提及任何东西。

我遇到的问题是我已经建立了两个链接(一个跟随,另一个取消关注)链接工作,当点击时记录被添加并从数据库中删除完全但是当我点击刷新时动作发生无论如何没有我点击链接。

尽可能清楚问题是:

如果用户当前被关注并且我刷新页面将取消关注(不点击按钮)..然后当他们被取消关注并且我刷新页面时他们现在将再次被跟踪(再次没有点击按钮)

这是代码:

<% if current_user.follows?(@user)%>
   <%= link_to "Unfollow", user_path, :action => current_user.unfollow!(@user), :class => "btn btn-primary"%>
<% else %>
   <%= link_to "Follow", user_path, :action => current_user.follow!(@user), :class => "btn btn-primary"%>
<% end %>

我认为它与之相关:浏览器缓存链接或链接生成的事实

<a href="/users/1" action="#&lt;Follow:0x103ce81d8&gt;" class="btn btn-primary" rel="nofollow">Follow</a>

并且操作以任何方式执行

编辑:

Rake routes: 

  users_index GET    /users/index(.:format)            users#index
         dashboard_index GET    /dashboard/index(.:format)        dashboard#index
    dashboard_my_rentals GET    /dashboard/my_rentals(.:format)   dashboard#my_rentals
    dashboard_my_credits GET    /dashboard/my_credits(.:format)   dashboard#my_credits
    dashboard_my_invites GET    /dashboard/my_invites(.:format)   dashboard#my_invites
      dashboard_my_faves GET    /dashboard/my_faves(.:format)     dashboard#my_faves
  dashboard_edit_profile GET    /dashboard/edit_profile(.:format) dashboard#edit_profile
           tsmhome_index GET    /tsmhome/index(.:format)          tsmhome#index
        new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           devise/registrations#cancel
       user_registration POST   /users(.:format)                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)             devise/registrations#edit
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/registrations#destroy
              home_index GET    /home/index(.:format)             home#index
                   users GET    /users(.:format)                  users#index
                         POST   /users(.:format)                  users#create
                new_user GET    /users/new(.:format)              users#new
               edit_user GET    /users/:id/edit(.:format)         users#edit
                    user GET    /users/:id(.:format)              users#show
                         PUT    /users/:id(.:format)              users#update
                         DELETE /users/:id(.:format)              users#destroy
                                /show/:id(.:format)               user#show
                    root        /                                 home#index



user controller

  def index
@users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :json => @users }
end


end
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :json => @user }
    end
  end

1 个答案:

答案 0 :(得分:0)

在您的链接action被视为attribute的{​​{1}}。因为link_to tag应该作为第二个参数给出。检查doc。要获取控制器的特定操作的路径,请执行action。它会列出rake routes并使用相同的内容,并在routes的最后附加_path

我认为它可能如下所示:

link_to tag

在控制器中:

<% if current_user.follows?(@user)%>
   <%= link_to "Unfollow", unfollow_user_path(:user => @user), :class => "btn btn-primary"%>
<% else %>
   <%= link_to "Follow", follow_user_path(:user => @user), :class => "btn btn-primary"%>
<% end %>

在路线中:

def follow
  #do something like
  user = User.find(params[:user])
  current_user.follow!(user)
  flash[:success] = "Follwed user #{user.name}"
  redirect_to :back
end

def unfollow
  #do something like
  user = User.find(params[:user])
  current_user.unfollow!(user)
  flash[:success] = "Unfollwed user #{user.name}"
  redirect_to :back
end