【问题标题】:Having some error issues about designing a blog in Ruby on Rails在 Ruby on Rails 中设计博客时遇到一些错误问题
【发布时间】: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


【解决方案1】:

错误是告诉您在您的ArticlesController 中尝试生成文章显示路由时缺少:id 参数。

config/routes 文件中的resources :articles 行将生成许多路由辅助方法,包括用于显示路由的article_path。默认情况下,此辅助方法需要一个参数 - 一个可以转换为上述:id 参数的参数。此参数应该是文章 ID,或者更常见的是文章的实例。您需要告诉 Rails 要将用户发送到哪个文章显示页面,对吗?

因此,您需要在您的create 操作中将文章实例传递给您对article_path 的调用(并显示update)。这是您的 create 操作的重写:

def create
  @article = Article.new(article_params)
  @article.save
  redirect_to article_path(@article)
end

【讨论】:

  • 这确实解决了访问节目的问题,但不是真正的问题。我只是认为问题与要保存的文章的 :image 有关。如果我尝试加载图像,则不会保存任何内容,并且会遇到相同的问题。如果我不包含图像,它会被保存并正常运行。有人有使用回形针的经验吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
相关资源
最近更新 更多