2013-02-26 75 views
0

我一直試圖在網上找到這個答案,但沒有運氣,也許這會幫助別人。爲什麼Rails在刷新時不執行動作而沒有點擊鏈接

我安裝了一款名爲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="https://stackoverflow.com/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 
+0

問題與UR鏈接行動。檢查使用'耙路徑路線'爲'follow'和'unfollow'。如果你沒有找到解決的辦法,可以發佈你的'controller methods'和'routes details' .. – codeit 2013-02-26 15:45:27

回答

0

在你的鏈接action被視爲link_to tagattribute。因爲action應該作爲第二個參數給出。檢查doc。爲了獲得控制器的具體行爲的路徑,請執行rake routes。它將列出routes,並在link_to tag末尾附加_path

我覺得它可能看起來像下面:

<% 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 

在路線:

resources :users do 
    collection do 
    get 'follow' 
    get 'unfollow' 
    end 
end 
+0

我明白!我現在會嘗試。 – 3cool4school 2013-02-26 16:01:11

+0

你有什麼控制器? 「follow」和「unfollow」方法在哪裏定義?他們是在模型中定義的? – codeit 2013-02-26 16:02:00

+0

控制器被禁止 – 3cool4school 2013-02-26 16:03:59

相關問題