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

维沙尔贾特

文章_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

路由文件

'''

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您将 routes.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>

'''

新的.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 %>

'''

模式文件

'''

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

'''

数据库.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

'''

我不知道我哪里出错了。请在您回复时更改上述代码。

纳齐尔

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

 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章