Ruby on rails 后续和后续事件
我已经完成了MichaelHartlRubyonRails 5教程,现在正试图将代码应用到我自己的应用程序中 我想做的是:Ruby on rails 后续和后续事件,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我已经完成了MichaelHartlRubyonRails 5教程,现在正试图将代码应用到我自己的应用程序中 我想做的是: 跟踪/取消跟踪事件 显示此事件后的用户总数 用户可以看到他们正在关注的所有事件 当前错误出现在我尝试呈现当前用户正在跟踪的会议时 我得到的错误是: ActionController::UrlGenerationError in Users#show No route matches {:action=>"followers", :controller=>"c
- 跟踪/取消跟踪事件
- 显示此事件后的用户总数
- 用户可以看到他们正在关注的所有事件
ActionController::UrlGenerationError in Users#show
No route matches {:action=>"followers", :controller=>"conferences", :id=>nil} missing required keys: [:id]
导致问题的线路是:
<a href="<%= followers_conference_path(@conference) %>">
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
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
CONFERENCES\u CONTROLLER.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
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
class ConferencesController
啊,我明白了
检查您的用户控制器#show
,查看是否在任何地方设置了@conference
如果不尝试:
啊,我看到问题了
检查您的用户控制器#show
,查看是否在任何地方设置了@conference
如果不尝试:
我敢肯定,您不想使用用户id获取@conference对象。相反,您可能正在生成用户正在关注的会议列表。在这种情况下,你会使用这样的东西
<% @user.conferences.each do |conference| %>
<a href="<%= followers_conference_path(conference) %>"><%= conference.name %></a>
<% end %>
我敢肯定,您不想使用用户id获取@conference对象。相反,您可能正在生成用户正在关注的会议列表。在这种情况下,你会使用这样的东西
<% @user.conferences.each do |conference| %>
<a href="<%= followers_conference_path(conference) %>"><%= conference.name %></a>
<% end %>
您能发布导致错误的行吗?不是路由,
\u path
方法可能需要IDpassed@Ruslan我真蠢,不加上它。我加了一句,我明白了。检查您的用户控制器#show
,查看是否在任何地方设置了@conference
。如果没有,请尝试:
@Ruslan感谢您的回复。它没有显示在UsersController#show中。你能告诉我(甚至解释一下)我应该如何在UsersController#show中编写它吗?谢谢大家!@臭名昭著的信条您在哪里添加了这一行,即在哪个视图中?我感觉的问题是,如果它位于像new这样的路径上,会议刚刚初始化但没有保存,所以它没有Id。或者它甚至还不存在。你能发布导致错误的行吗?不是路由,\u path
方法可能需要IDpassed@Ruslan我真蠢,不加上它。我加了一句,我明白了。检查您的用户控制器#show
,查看是否在任何地方设置了@conference
。如果没有,请尝试:
@Ruslan感谢您的回复。它没有显示在UsersController#show中。你能告诉我(甚至解释一下)我应该如何在UsersController#show中编写它吗?谢谢大家!@臭名昭著的信条您在哪里添加了这一行,即在哪个视图中?我感到的问题是,如果它位于像new这样的路径上,会议刚刚初始化,但没有保存,因此它没有Id。或者它甚至还不存在。我已将@conference=conference.find(params[:Id])
添加到userscocontroller#show
中,现在它正在工作。非常感谢你!!!我已经将@conference=conference.find(params[:id])
添加到userscocontroller\show
中,现在它开始工作了。非常感谢你!!!谢谢你的回复,我今天会尝试一下,然后再回复你!谢谢你的回复,我今天会尝试一下,然后再回复你!