2017-03-28 41 views
0

我已經完成了Michael Hartl Ruby on Rails 5教程,現在正在嘗試將代碼應用到我自己的應用程序。關注並取消關注活動

我試圖做的是:

  • 關注/取消關注的事件
  • 顯示的用戶總數此事件
  • 用戶可以看到所有他們遵循
  • 事件以下

當我嘗試呈現當前用戶正在關注的會議時,會顯示當前錯誤。

,我得到的錯誤是:

ActionController::UrlGenerationError in Users#show 
No route matches {:action=>"followers", :controller=>"conferences", :id=>nil} missing required keys: [:id] 

引起該問題的行是:

<a href="<%= followers_conference_path(@conference) %>"> 

現在很明顯,他們是一些錯誤的,我的路由,我認爲以下問題指出,會議缺少必需的ID?

有誰知道我的問題的解決方案?這是越來越讓用戶跟蹤事件,看看他們是什麼如下事件

用戶控制器SHOW

def show 
    @user = User.find(params[:id]) 
    @microposts = @user.microposts.paginate(page: params[:page]) 
    @managments = @user.managments.paginate(page: params[:page]) 
    @conference = Conference.find(params[:id]) 
    end 

的routes.rb

Rails.application.routes.draw do 

    root 'static_pages#home' 

    get '/help', to: 'static_pages#help' 
    get '/about', to: 'static_pages#about' 
    get '/contact', to: 'static_pages#contact' 

    get '/my_conference', to: 'my_conference#show' 
    get '/conferences', to: 'conferences#index' 
    get '/conferences_list', to: 'conferences#index_admin' 

    get '/my_employees', to: 'employees#index' 

    get '/signup', to: 'users#new' 

    get '/login', to: 'sessions#new' 
    post '/login', to: 'sessions#create' 
    delete '/logout', to: 'sessions#destroy' 

    get '/login', to: 'sessions#new' 

    put 'activate/:id(.:format)', :to => 'users#activate', :as => :activate_user 
    put 'deactivate/:id(.:format)', :to => 'users#deactivate', :as => :deactivate_user 

    put 'activate_employee/:id(.:format)', :to => 'employees#activate', :as => :activate_employee 
    put 'deactivate_employee/:id(.:format)', :to => 'employees#deactivate', :as => :deactivate_employee 

    resources :users do 
    member do 
     get :following 
    end 
    end 
    resources :conferences do 
    member do 
     get :followers 
    end 
    end 
    resources :articles 
    resources :users 
    resources :employees 
    resources :account_activations, only: [:edit] 
    resources :activations, only: [:edit] 
    resources :password_resets,  only: [:new, :create, :edit, :update] 
    resources :microposts,   only: [:create, :destroy] 
    resources :managments 
    resources :conferences 
    resources :relationships,  only: [:create, :destroy] 


end 

CONFERENCES_CONTROLLER.RB

class ConferencesController < ApplicationController 
    before_action :logged_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: :destroy 
    before_action :admin_user,  only: :destroy 

    def index 

     @conferences = Conference.paginate(page: params[:page]) 

     if params[:search] 
     @conferences = Conference.search(params[:search]).order("created_at DESC").paginate(page: params[:page]) 
     else 
     @conferences = Conference.all.order('created_at DESC').paginate(page: params[:page]) 
     end 
    end 

    def new 
    @user = User.new 
    @conference = Conference.new 
    end 

    def create 

    @conference = current_user.conferences.build(conference_params) 
    if @conference.save 
     flash[:success] = "conference created!" 
     redirect_to conferences_path 
    else 
     @feed_items = current_user.feed.paginate(page: params[:page]) 
     render 'new' 
    end 
    end 

    def destroy 
    @conference.destroy 
    flash[:success] = "conference deleted" 
    redirect_to request.referrer || root_url 
    end 

    def following 
    @title = "Following" 
    @conference = Conference.find(params[:id]) 
    @conferences = @conference.following.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    def followers 
    @title = "Followers" 
    @user = User.find(params[:id]) 
    @users = @user.followers.paginate(page: params[:page]) 
    render 'show_follow' 
    end 


    private 

    def conference_params 
     params.require(:conference).permit(:conference,:country , :month, :presence, :audience, :cost ,:picture) 
    end 

    # Confirms an admin user. 
    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 

    def correct_user 
     @conference = current_user.conferences.find_by(id: params[:id]) 
     redirect_to root_url if @conference.nil? 
    end 

end 
+0

你可以發佈導致錯誤的行嗎?這不是路線,'_path'方法可能需要一個ID傳遞 – Ruslan

+0

@Ruslan當然我不會添加它。我已經加了 – Salman

+0

啊,我看到了這個問題。檢查你的'UsersController#show'來查看'@ conference'是否被設置在任何地方。如果不嘗試:' Ruslan

回答

2

啊,我看到了這個問題。

檢查您的UsersController#show,看看是否在任何地方設置了@conference

如果不嘗試:<a href="<%= followers_conference_path(current_user.id) %>">

+1

我已將'@conference = Conference.find(params [:id])'添加到'UsersController#show'和現在它正在工作。非常感謝 !!! – Salman

1

您不想使用用戶ID來獲得@conference對象,我敢肯定。相反,您可能正在生成用戶所關注的會議列表。在這種情況下,你會使用這樣的事情

<% @user.conferences.each do |conference| %> 
    <a href="<%= followers_conference_path(conference) %>"><%= conference.name %></a> 
<% end %> 

而且,在Rails中,你通常會使用link_to幫手,也許包括計數顯示會議有多少追隨者了。

<% @user.conferences.each do |conference| %> 
    <%= link_to conference.name, followers_conference_path(conference) %> 
    (<%= pluralize conference.users.count, 'follower' %>) 
<% end %> 
+0

謝謝你的迴應,我今天會試一試,回覆你! – Salman