Salman
Salman

Reputation: 1024

Following and unfollowing events

I've done Michael Hartl Ruby on Rails 5 tutorial and am now trying to apply the code to my own app.

What I'm trying to do is:

The current error appears when I try to render the conferences that current user is following.

The error that I get is:

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

The line that causes the problem is:

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

Now obviously their is something wrong in my routing and I assume the following problem states that conferences is missing a required id?

Does anyone know the solution to my problem? Which is getting allowing users to follow events and see what events they are following

USER CONTROLLER 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

Upvotes: 0

Views: 101

Answers (2)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

You don't want to use the user id to get the @conference object, I'm sure. Instead, you are probably generating a list of conferences the user is following. In that case, you'd use something like this

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

And, in Rails, you would usually use the link_to helper, and maybe include a count to show how many followers the conference has.

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

Upvotes: 1

Ruslan
Ruslan

Reputation: 2009

Ahh, i see the issue.

Check your UsersController#show to see if @conference is being set anywhere.

If not try: <a href="<%= followers_conference_path(current_user.id) %>">

Upvotes: 2

Related Questions