0

Rails novice Ques.I'm not able to find the reason for why my strong parameters are not getting saved in the database. I'm not using scaffolding to create my models rather using the rails g model customer name:string email:string command. The same gets added when using rails console.

My customer.rb
class Customer < ActiveRecord::Base
    has_many :products,through: :order
end

My usercontroller

class UserController < ApplicationController

  def login
    @Customer=Customer.new(master)
    @Customer.save
  end

  def home
  end

  private

  def master
    params.require(:customer).permit!
  end
end

my html code:
<form action="login">
    name <input type="text" name="customer[name]">
    email<input type="email" name="customer[email]">
    <input type="submit" value="enter">
</form>

Still i get the error of param :customer not found

user2526795
  • 77
  • 1
  • 8

2 Answers2

0

The lines

@Customer=Customer.new(master)
@Customer.save

should be

@customer=Customer.new(master)
@customer.save

If you want I can paste in equivalent line from one of the controllers in the application I am working on. Pierre

rb512
  • 6,880
  • 3
  • 36
  • 55
user1854802
  • 388
  • 3
  • 14
0

As addition on answer of user1854802, you should also take an another look to the params.

With the #permit! method you're allowing mass assignment. So any parameter given in the params hash will be allowed. Use the #permit (without a bang !) method instead to filter for the attributes you want to allow in your params. For example:

params.require(:customer).permit(:name, :email)

That ensures that the parameter customer is present in params and that only the email and name attributes of the customer are allowed.

Community
  • 1
  • 1
JanDintel
  • 999
  • 7
  • 11