shravan
shravan

Reputation: 97

No entry has been saved to database in ruby on rails?

I have created a table name tenants which have following column

class CreateTenants < ActiveRecord::Migration
  def change
    create_table :tenants do |t|
      t.text :company_name
      t.text :work_area
      t.text :second_pref
      t.text :third_pref
      t.integer :who_are_you
      t.integer :number_of_bedroom
      t.text :other_specs
      t.string :budget
      t.string :name
      t.string :email
      t.string :contact_number

      t.timestamps null: false
    end
  end
end

And i am entering the data into in table by following form

        <%= form_for Tenant.new do |val| %>

                <%= val.label :company_name, "Company Name" %>
                <%= val.text_field :company_name, class: 'form-control' %>

                <%= val.label :work_area, "Work Area" %>
                <%= val.text_field :work_area, class: 'form-control' %>

                <%= val.label :second_pref, "Second Preference" %>
                <%= val.text_field :second_pref, class: 'form-control' %>

                <%= val.label :third_pref, "Third Preference" %>
                <%= val.text_field :third_pref, class: 'form-control' %>

                <%= val.label :who_are_you, "Are you Family/Bachelor?" %>
                <%= val.text_field :who_are_you, class: 'form-control' %>

                <%= val.label :number_of_bedroom, "Number of Bedroom" %>
                <%= val.text_field :number_of_bedroom, class: 'form-control' %>

                <%= val.label :other_specs, "Other Requirments" %>
                <%= val.text_field :other_specs, class: 'form-control' %>

                <%= val.label :budget, "Your Budget" %>
                <%= val.text_field :budget, class: 'form-control' %>

                <%= val.label :name, "Name" %>
                <%= val.text_field :name, class: 'form-control' %>

                <%= val.label :email, "Email" %>
                <%= val.text_field :email, class: 'form-control' %>

                <%= val.label :contact_number, "Contact Number" %>
                <%= val.text_field :contact_number, class: 'form-control' %>

                <%= val.submit "Submit", class: "btn btn-primary" %>

    <% end %>

When i fill all the required filled and click submit, i see the following output in rails server log.

Started POST "/tenants" for ::1 at 2015-11-15 11:41:27 +0530
Processing by TenantsController#create as HTML
  Parameters: {
"utf8"=>"✓", "authenticity_token"=>"26KYMFmofF+A1UrF+eWu21nEGbVO3n2bUSPl8340k8hY1JQhYF2kfhOHLmlF+r1Tj5UB7h6H+IJ7MY+Rx+o4CA==",
"tenant"=>
  {
  "company_name"=>"Housing.com",
  "work_area"=>"Hiranandani Business Park",
  "second_pref"=>"Chandivali",
  "third_pref"=>"Vikhroli",
  "who_are_you"=>"Bachelor",
  "number_of_bedroom"=>"3",
  "other_specs"=>"Gym, Swimming Pool",
  "budget"=>"55000",
  "name"=>"Shravan Kumar Gond",
  "email"=>"[email protected]",
  "contact_number"=>"9475593772"
  },
"commit"=>"Submit"
}
Unpermitted parameter: budget
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction

Can anyone tell me, why this happening ?

Upvotes: 3

Views: 54

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Unpermitted parameter: budget

This is your error - it means you're passing parameters to your controller, but it cannot save them in your model.

The fix is to set the strong params method (as shravan40 suggested).


Since you're calling Tenant.new in your #new action, I would recommend using the following in your controller:

#app/controllers/tenants_controller.rb
class TenantsController < ApplicationController
   def new
      @tenant = Tenant.new
   end 
   def create
      @tenant = Tenant.new tenant_params
      @tenant.save
   end

   private

   def tenant_params
      params.require(:tenant).permit(:company_name, :work_area, :second_pref, :third_pref, :who_are_you, :number_of_bedroom, :other_specs, :budget, :name, :email, :contact_number)
   end
end

There's also something you can do to polish up your form...

<%= form_for @tenant do |val| %>
   <% vals = [[:company_name],[:work_area],[:second_pref, "Second Preference"], [:third_pref, "Third Preference"],[:who_are_you, "Are you Family/Bachelor?"], [:number_of_bedroom], [:other_specs, "Other Requirements"],[:budget, "Your Budget"], [:name], [:email], [:contact_number]] %> 

   <% vals.each do |value| %>
       <% value[1] ||= value[0].to_s.gsub("_", " ") %>
       <%= val.label value[0], value[1] %>
       <%= val.text_field value[0], class: 'form-control' %>
   <% end %>

   <%= val.submit "Submit", class: "btn btn-primary" %>
<% end %>

Upvotes: 2

Shravan40
Shravan40

Reputation: 9888

It seems you are missing :budget in strong parameters of your tenants_controller.rb file. It should be something like this.

private

    def tenant_params
        params.require(:tenant).permit(:company_name, 
                                   :work_area, 
                                   :second_pref, 
                                   :third_pref, 
                                   :who_are_you, 
                                   :number_of_bedroom, 
                                   :other_specs,
                                   :budget,
                                   :name, 
                                   :email, 
                                   :contact_number)
    end

Upvotes: 3

Related Questions