2017-08-24 3 views
0

私は自分のアプリケーションに問題があります。私は今あなたにアプリケーションのコードを渡します。エラーの画像です。これは私の仕事です。 Ruby on RailsからWebアプリケーションを作成するには、記事を作成してデータベースに保存する必要があります。ArticlesControllerのNoMethodErrorは、nilのための未定義メソッドsaveを作成します。NilClass

これは誤差の画像です:https://i.stack.imgur.com/hYTkl.png

私の雲9コード

routes.rbを:

Rails.application.routes.draw do 
# The priority is based upon order of creation: first created -> highest 
priority. 
# See how all your routes lay out with "rake routes". 

# You can have the root of your site routed with "root" 
# root 'welcome#index' 
resources :articles 

root 'pages#home' 
get 'about', to: 'pages#about' 

article.rb:

class Article < ActiveRecord::Base 

end 

articles_controller.rb :

class ArticlesController < ApplicationController 

    def new 
    @article = Article.new 
    end 
    def create 
     #render plain: params[:article].inspect 
    @article.save 
    redirect_to_articles_show(@article) 
    end 
    private 
    def article_params 
    params.require(:article).permit(:title, :description) 


    end 





end 

new.html.erb:

class CreateArticles < ActiveRecord::Migration 
    def change 

     create_table :articles do |t| 
     t.string :title 
     t.text :description 

    end 
    end 
end 

私のschema.rb:

ActiveRecord::Schema.define(version: 20170820190312) do 

    create_table "articles", force: :cascade do |t| 
    t.string "title" 
    t.text "description" 
    end 

end 

答えて

0

を試してみてください。

はそうのようなあなたのcreate方法を更新してみてください。

def create 
    @article = Article.new(article_params) 

    @article.save 
    redirect_to @article 
end 

私はこれが役に立てば幸い!

0

あなた@article

記事

<%= form_for @article do |f| %> 

<p> 
    <%= f.label :title %> 

    <%= f.text_field:title %> 

</p> 
<p> 
    <%= f.label :description %> 
    <%= f.text_area :description %> 

</p> 
<p> 
    <%= f.submit %> 

</p> 
    <% end %> 

私の移行ファイルを作成します。 〜でアクションの作成はnilです。あなたはそれを保存する前にcreate methodでオブジェクトをインスタンス化する必要があり、この1

def create 
    @article = Article.new(article_params) 
    @article.save 
    redirect_to_articles_show(@article) 
end 
関連する問題