我试图使用回形针将Images包含在我的博客设计中。我不断收到错误消息:ArticlesController#create中的ActionController :: UrlGenerationError没有路由匹配{:action =>“ show”,:controller =>“ articles”}缺少必需的键:[:id]现在,每当我单击提交时按钮创建新文章,并显示“找不到没有ID的文章”。我也尝试通过链接访问显示页面的视图,但我做不到。
提取的来源(第30行附近):
这是我的文章负责人
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comment.article_id = @article.id
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
@article.save
redirect_to article_path
end
def edit
@article = Article.find(params[:id])
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
def update
@article = Article.find(params[:id])
@article.update(article_params)
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path
end
def article_params
params.require(:article).permit(:title, :body, :tag_list, :image)
end
end
这是我的文章助手:
module ArticlesHelper
def article_params
params.require(:article).permit(:title, :body, :tag_list, :image)
end
end
这是我的文章/show.html.erb
<h1><%= @article.title %></h1>
<p>
Tags:
<% @article.tags.each do |tag| %> <%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
<% if @article.image.exists? %>
<p><%= image_tag @article.image.url %></p>
<% end %>
<p><%= @article.body %></p>
<h3>Comments (<%= @article.comments.size %>)</h3>
<%= render partial: 'articles/comment', collection: @article.comments %>
<%= render partial: 'comments/form' %>
<%= link_to "<< Back to Articles List", articles_path %>
<%= link_to "delete", article_path(@article), method: :delete, data: {confirm: "Really delete the article?"} %>
<%= link_to "edit", edit_article_path(@article) %>
这是我的路线文件
TheBlog::Application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get 'article' => 'articles#show'
resources :users
resources :articles do
resources :comments
end
resources :tags
end
错误提示您:id
尝试在中生成商品展示路线时缺少参数ArticlesController
。
将resources :articles
在您的在线config/routes
文件会产生许多途径的辅助方法,包括article_path
为展示途径。默认情况下,此辅助方法需要一个参数-该参数可以转换为上述:id
参数。此参数应该是商品ID,或更常见的是商品实例。您需要告诉Rails将用户发送到哪个文章显示页面,对吗?
因此,您需要article_path
在create
操作中将文章实例传递给您的调用(update
它会出现)。这是您的create
操作的重写:
def create
@article = Article.new(article_params)
@article.save
redirect_to article_path(@article)
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句