2014-10-01 43 views
1

我要創建「文章」的實例和瀏覽器給我一個錯誤錯誤「參數丟失或爲空值:文章」

參數是丟失或爲空值:文章

這裏我的文章控制器

class ArticlesController < ApplicationController 

    def index 
     @article = Article.all 
    end 

    def new 
      @article = Article.new 
    end 

    def create 
     @article = Article.new(article_params) 
    if @article.save 
    redirect_to @article 
    else 
    render 'new' 
    end 
end 

    def show 
     @article = Article.find(params[:id]) 
    end 

    private 
    def article_params 
    params.require(:article).permit(:title, :body) 
    end 
end 

,如何解決?

回答

0

這是引發錯誤的方法。

def article_params 
    params.require(:article).permit(:title, :body) 
end 

這意味着你沒有將這樣的參數傳遞給控制器​​。確保你在新/編輯視圖中有一個表單,並且正確地傳遞了它的值。

+0

我明白error.But的這種方法,我認爲這是所有真正的,和我做了一個記錄被保存在數據庫中,並顯示在頁面,我決定創建另一個記錄和一個錯誤 – 138weare 2014-10-01 21:04:30

1

此錯誤表示article參數在新窗體中缺失或爲空。進入project/views/new.html.erb並確保您實際上將:article傳遞到url:articles_path

<%= form_for :*article*, url: articles_path do |f| %> 

我在文章中有一個拼寫錯誤(意外地輸入文章),它產生了完全相同的錯誤。

0

試試這個代碼,它的工作對我來說:

def article_params 
    params.require(:articles).permit(:title, :body) 
end 
相關問題