Simon Cooper
Simon Cooper

Reputation: 1594

Rails ActionController::ParameterMissing

In Rails 5, I'm following the getting started guide on http://guides.rubyonrails.org/, and have implement the Articles taks fine. I'm now trying to replicate for Coffeeshops. However when I try and submit the form on localhost, the console tells me I have an empty or missing parameter running @coffeeshop returns 'nill', so I assume it's empty.

My migration file:

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

My Controller:

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

And my routes:

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

Here's my form:

<%= 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 %>

I've checked typos/spellings, tried changing params.require(:coffeeshop) to coffeeshop_params.require(:coffeeshop) but can't spot what's causing it. I've run rails db:migrate.

What have I overlooked?

Upvotes: 0

Views: 43

Answers (1)

mikdiet
mikdiet

Reputation: 10038

You need to use instead

Coffeeshop.find(coffeeshop_params[:id])

this

Coffeeshop.find(params[:id])

because id is a top level param

Upvotes: 2

Related Questions