我已经完成了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?
有谁知道我的问题的解决方案?哪个允许用户关注事件并查看他们正在关注的事件
用户控制器显示
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 :(得分:2)
啊,我看到了这个问题。
检查您的UsersController#show
,看看是否在任何地方设置@conference
。
如果没有尝试:<a href="<%= followers_conference_path(current_user.id) %>">
答案 1 :(得分: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 %>