2014-10-30 59 views
0

我試圖用回形針將圖片添加到我的博客設計中。我不斷收到錯誤: ArticlesController中的ActionController :: UrlGenerationError#create 沒有路由匹配{:action =>「show」,:controller =>「articles」}缺少必需的鍵:[:id] 現在,在提交按鈕上創建一篇新文章,它會顯示「找不到沒有ID的文章」。我嘗試通過鏈接訪問顯示頁面的視圖,但我無法訪問。在Ruby on Rails中設計博客時出現了一些錯誤問題

提取的源(左右線#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 
+0

這是在一個測試或從運行開發服務器? – AJcodez 2014-10-30 23:25:42

+0

請包括您的ArticlesController創建代碼 – 2014-10-31 00:59:03

回答

0

錯誤是告訴你,你正在試圖產生在ArticlesController文章顯示路線時失蹤的:id參數。

resources :articles在你的config/routes文件行中會產生一些幫助方法的路由,其中​​包括article_path用於顯示路由。默認情況下,這個幫助器方法需要一個參數 - 一個參數可以翻譯成前面提到的:id參數。這個論點應該是文章ID,或者更常見的是文章的一個實例。你需要告訴Rails將哪些文章展示頁面發送給用戶,對吧?

因此,您需要將012-動作(和出現的update)的文章實例傳遞給article_path。這裏是你的create行動的重寫:

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

這絕對解決了訪問節目的問題,但不是真正的問題。我剛纔發現問題與要保存的文章的圖像有關。如果我嘗試加載圖像,則不會保存任何內容,並且會遇到同樣的問題。如果我不包含圖像,它會被保存並正常運行。任何人都有使用回形針的經驗嗎? – codigomonstruo 2014-10-31 01:45:25

+0

反正,謝謝!我正在上傳錯誤的圖片格式 – codigomonstruo 2014-10-31 02:07:19

相關問題