Ruby on rails 没有导致ActionController UrlGenerationError的路由匹配
我根据Hartl的教程创建了一个ToDoList,并遵循和添加了一个标记系统。我一直遵循到第10节,他们要求我将Ruby on rails 没有导致ActionController UrlGenerationError的路由匹配,ruby-on-rails,ruby,routes,Ruby On Rails,Ruby,Routes,我根据Hartl的教程创建了一个ToDoList,并遵循和添加了一个标记系统。我一直遵循到第10节,他们要求我将new.html.erb文件修改为源代码所示的代码。为了临时解决代码中的结构差异,我会编辑一些其他文件,比如在本例中,我的microspost_form partial。偶尔,我会在视频中的代码和文字教程中的代码之间切换,因为其中一些代码会产生错误消息或不会产生所需的功能。以下是我认为与这个问题有关的文件 _micropost_form.html.erb(将显示在用户主页上的填充表单)
new.html.erb
文件修改为源代码所示的代码。为了临时解决代码中的结构差异,我会编辑一些其他文件,比如在本例中,我的microspost_form partial
。偶尔,我会在视频中的代码和文字教程中的代码之间切换,因为其中一些代码会产生错误消息或不会产生所需的功能。以下是我认为与这个问题有关的文件
_micropost_form.html.erb(将显示在用户主页上的填充表单)
micropost_控制器
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def index
params[:tag] ? @microposts = Micropost.tagged_with(params[:tag]) : @microposts = Micropost.all
end
def show
@micropost = Micropost.find(params[:id])
end
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "You have deleted a task!"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content, :tag_list, :tag,
{tag_ids: [] }, :tag_ids)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
class StaticPagesController < ApplicationController
def home
if logged_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
def help
end
def about
end
def contact
end
end
有人能提出这里可能出了什么问题吗?如果需要更多信息,请告诉我
更新:我已经实现了下面答案所提供的一些更改,但仍然不理解为什么未检测到:tag
,以及为什么红色代码实际高亮显示
ActionController::UrlGenerationError in StaticPages#home
No route matches {:action=>"index", :controller=>"microposts", :tag=>nil}, missing required keys: [:tag]
问题是你没有一个针对你的微博客的索引路线。
Rails.application.routes.draw do
root 'static_pages#home'
get '/readme', to: 'static_pages#readme'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/users/admin', to: 'users#admin'
resources :users
resources :microposts, only: [:create, :destroy] #Here's the problem
get 'tags/:tag', to: 'microposts#index', as: :tag
end
更改为:
resources :microposts, only: [:index, :create, :destroy]
编辑:
另一个问题是
if logged_in?
@micropost = current_user.microposts.build #this just returns a new 1
@feed_items = current_user.feed.paginate(page: params[:page])
end
您可能想要这样的东西:
if logged_in?
@microposts = current_user.microposts
@feed_items = Micropost.all.paginate(page: params[:page])
end
这将为您提供所有用户的微操作。然后在视图中迭代它们。我发现问题的原因其实很简单。在运行
rails控制台
之后,我的db:seed似乎没有被正确地耙过,导致我的标签有nil
名称,并导致我无法找到路由。为了进一步解决种子添加问题,我意识到我已经添加了attr\u accessor
,忘记了普通属性应该通过命令行添加到迁移中,而不是直接写入模型中。根据帖子删除它会更新我的数据库,代码也会工作。什么时候会出现错误?在表单提交时还是在控制器内部重定向时?使用pry
gem进行调试&同时请注意,您的操作正在尝试使用缺少的标记id进行Micropost#索引。路由是不正确的,我猜我在运行rails服务器
并到达0.0.0.0:3000
时会出错。当我尝试访问索引时,我看不到如何使用标记id
,对不起,rails是新来的:)错误在您的路由中。rb
。确保从何处生成错误的完整日志您所说的完整日志是什么意思?我知道它来自routes.rb
,但不知道为什么它没有提供:tag
嗨,Helsing,谢谢你的回答,但是更改它并没有更改我的错误消息,但是谢谢你提醒我更新代码我看到了。似乎标签有问题。如果仍然没有解决,我将在今天晚些时候查看。非常感谢您事先提供的帮助。@micropost=current_user.micropost.build为您提供了一个没有任何标记的新micropost,这可能就是您看到它的原因。可能存在多个问题,因此,一旦清除其中一个问题,您就会遇到另一个错误。感谢您的建议,我确实遇到了另一个错误,即未定义的方法#
<% if @feed_items.any? %>
<ol class="microposts">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
ActionController::UrlGenerationError in StaticPages#home
No route matches {:action=>"index", :controller=>"microposts", :tag=>nil}, missing required keys: [:tag]
app/views/microposts/_micropost.html.erb:5:in `block in _app_views_microposts__micropost_html_erb___3891111682689684005_70324923859580'
app/views/microposts/_micropost.html.erb:5:in `map'
app/views/microposts/_micropost.html.erb:5:in `_app_views_microposts__micropost_html_erb___3891111682689684005_70324923859580'
app/views/shared/_feed.html.erb:3:in `_app_views_shared__feed_html_erb__3168328449514417483_70324923896060'
app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__3511776991923566869_70324898321240'
ActionController::UrlGenerationError in StaticPages#home
No route matches {:action=>"index", :controller=>"microposts", :tag=>nil}, missing required keys: [:tag]
Rails.application.routes.draw do
root 'static_pages#home'
get '/readme', to: 'static_pages#readme'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/users/admin', to: 'users#admin'
resources :users
resources :microposts, only: [:create, :destroy] #Here's the problem
get 'tags/:tag', to: 'microposts#index', as: :tag
end
resources :microposts, only: [:index, :create, :destroy]
if logged_in?
@micropost = current_user.microposts.build #this just returns a new 1
@feed_items = current_user.feed.paginate(page: params[:page])
end
if logged_in?
@microposts = current_user.microposts
@feed_items = Micropost.all.paginate(page: params[:page])
end