Rails ActionController :: ParameterMissing

时间:2017-04-03 15:56:20

标签: ruby-on-rails

在Rails 5中,我正在关注http://guides.rubyonrails.org/的入门指南,并且已经实施了文章。我现在正在尝试为Coffeeshops复制。但是,当我尝试在localhost上提交表单时,控制台告诉我运行空的或缺少的参数@coffeeshop返回'nill',所以我认为它是空的。

我的迁移文件:

class CreateCoffeeshops < ActiveRecord::Migration[5.0]
  def change
    create_table :coffeeshops do |t|
      t.string :name
      t.text :desc
      t.text :area
      t.text :url
      t.text :email
      t.text :address
      t.text :postcode
      t.text :phone

      t.timestamps
    end
  end
end

我的控制器:

class CoffeeshopsController < ApplicationController
  def show
    @coffeeshop = Coffeeshop.find(coffeeshop_params[:id])
  end

  def new
  end

  def create
    @coffeeshop = Coffeeshop.new(coffeeshop_params)

    @coffeeshop.save
    redirect_to @coffeeshop
  end

  private
    def coffeeshop_params
      params.require(:coffeeshop).permit(:name, :desc, :area, :url, :email, 
:address, :postcode, :phone)
    end
end

我的路线:

Rails.application.routes.draw do

  get "/pages/:page" => "pages#show"
  resources :articles do
    resources :comments
  end

  resources :coffeeshops

  get 'home/index'
  root 'home#index'


# For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html
end

这是我的表格:

<%= form_for :coffeeshop, url: coffeeshops_path do |f| %>
  <p>
    <%= f.label :Name %><br>
    <%= f.text_field :name %>
  </p>

  <p>
    <%= f.label :Desciption %><br>
    <%= f.text_area :desc %>
  </p>

  <p>
    <%= f.label :Area %><br>
    <%= f.text_area :area %>
  </p>

  <p>
    <%= f.label :URL %><br>
    <%= f.text_area :url %>
  </p>

  <p>
    <%= f.label :email %><br>
    <%= f.text_area :email %>
  </p>

  <p>
    <%= f.label :Address %><br>
    <%= f.text_area :address %>
  </p>

  <p>
    <%= f.label :Postcode %><br>
    <%= f.text_area :postcode %>
  </p>

  <p>
    <%= f.label :Phone %><br>
    <%= f.text_area :phone %>
  </p>

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

我检查过拼写错误/拼写,尝试将params.require(:coffeeshop)更改为coffeeshop_params.require(:coffeeshop),但无法找出造成错误的原因。我跑了rails db:migrate

我忽略了什么?

1 个答案:

答案 0 :(得分:2)

您需要改为使用

Coffeeshop.find(coffeeshop_params[:id])

Coffeeshop.find(params[:id])

因为id是顶级参数