2017-04-03 46 views
0

在Rails 5中,我將按照http://guides.rubyonrails.org/的入門指南進行操作,並且已實施文章可以正常使用。我現在正在嘗試複製咖啡店。然而,當我嘗試在本地主機上提交表單時,控制檯告訴我有一個空的或缺少運行@coffeeshop的參數返回'nill',所以我認爲它是空的。Rails ActionController :: ParameterMissing

我的遷移文件:

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

我忽略了什麼?

回答

2

您需要使用代替

Coffeeshop.find(coffeeshop_params[:id]) 

Coffeeshop.find(params[:id]) 

因爲id是一個頂級PARAM

+0

嗯,這解決了它!謝謝。很近... –