未定义的局部变量或方法“ article_params”

时间:2019-11-30 13:21:13

标签: ruby-on-rails strong-parameters

articles_controller.rb

  class ArticlesController < ApplicationController

    def index
      @articles = Article.all
    end

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

    def new
    end

    def create
      @article = Article.new(article_params)

      if @article.save
        redirect_to @article
      else
        render new
    end

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

routes.rb

'''

Rails.application.routes.draw do
  #layout=false 
  get 'welcome/index'
  get 'welcome/second'
  get 'articles/new'
  post 'articles/new'
  #get 'articles/show'
  #get 'articles/index'
  #get 'articles/new'
  resources :articles
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root 'welcome#index'
end'

'''

show.html.erb 您将route.rb的内容复制到show.html.erb

index.html.erb '''

<h1>Listing articles</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
    </tr>
  <% end %>
</table>

'''

new.html.erb

'''

<%= form_for :article, url: articles_path do |form| %>
 <h1>new artical page</h1>
  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>

'''

schema.rb

'''

ActiveRecord::Schema.define(version: 2019_11_30_073138) do

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end

'''

database.yml

'''

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

'''

yymmddttss_create_articles.rb

'''

 class CreateArticles < ActiveRecord::Migration[6.0]
  def change
    create_table :articles do |t|
      t.string :title
      t.text :text

      t.timestamps
    end
  end
end

'''

我不知道我在哪里犯错。 请在回复时对上面的代码进行更改。

2 个答案:

答案 0 :(得分:0)

我认为您只需要删除控制器底部的一个“末端”即可

 class ArticlesController < ApplicationController

    def index
      @articles = Article.all
    end

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

    def new
    end

    def create
      @article = Article.new(article_params)

      if @article.save
        redirect_to @article
      else
        render new
    end

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

答案 1 :(得分:0)

您在这里错过了end

if @article.save
    redirect_to @article
  else
    render new
# here

因此,您认为它正在关闭方法的下一行实际上是在关闭“ if / else”块,并且您的article_params方法最终在create方法内部

编辑:还要删除end定义之后的最后一个article_params