【发布时间】:2014-10-30 23:22:42
【问题描述】:
我尝试使用回形针在我的博客设计中包含图片。我不断收到错误: 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
【问题讨论】:
-
这是在测试中还是来自正在运行的开发服务器?
-
请包含您的 ArticlesController 创建代码
标签: ruby-on-rails paperclip blogs