简体   繁体   中英

Ruby on Rails My Database always makes the foreign keys nil why?

This is my schema for Table Listing

create_table "listings", force: :cascade do |t|
  t.string "title"
  t.text "description"
  t.string "city"
  t.string "state"
  t.string "zipcode"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "category_id"
  t.integer "subcategory_id"
end

Here is my Model:

class Listing < ApplicationRecord
  belongs_to :category, required: false
  belongs_to :subcategory, required: false
end

Every time I enter a data via form, My data are recorded in Database with their assigned values but my foreign keys (category_id, subcategory_id) are always nil? why?

Here is my form: new.html.erb

<%= form_for @listing do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>
  <%= f.label :description %>
  <%= f.text_area :description %>
  <p>  <%= f.label :category_id %>
  <%= f.select :category_id, Category.all.map {|f| [f.name,f.id]} %></p>
  <p>  <%= f.label :subcategory_id %>
  <%= f.select :subcategory_id, Subcategory.all.map {|f| [f.name,f.id]} %></p>
  <%= f.label :city %>
  <%= f.text_field :city %>
  <%= f.label :state %>
  <%= f.text_field :state %>
  <%= f.label :zipcode %>
  <%= f.text_field :zipcode, class: "zip-width", maxlength: "5" %>
  <%= f.submit class:"create-button" %> 
<% end %>

Here is my controller:

class CategoriesController < ApplicationController

  def index
    @categories = Category.all
    @societal = @categories[0] #id 1 bhayekoharu
    @onsale = @categories[1]
    @housing = @categories[2]
    @works = @categories[3]
    @services = @categories[4]
    @personal = @categories[5]
  end

  def show
    @listings =  Listing.where(category_id: params[:id])

  end

My database shows following record:

    <Listing id:6, title: "apartment on rent", description: "rent 5 bed room\r\n800 per month\r\nno pets", city: "aust
        in", state: "tx", zipcode: "78749", created_at: "2018-06-21 00:02:40", updated_at: "2018-06-21 00:02:40
        ", category_id: nil, subcategory_id: nil>,

Here is my full controller for Listing:

class ListingsController < ApplicationController
  def new
    @listing = Listing.new
  end

  def create # we have to hold paramaters.
    @listing = Listing.new(listing_params)
    if @listing.save
      redirect_to root_path
    else
  # redisplay the form if validation failed
    render action: 'new'
    end
  end


  def show
    @listing = Listing.find(params[:id])
  end

  private
  def listing_params
    params.require(:listing).permit(:title, :description, :city, :state, :zipcode,:category, :subcategory) 
  end

end

One common mistake you might've done is not allowing those ids in strong params. Check the strong params in your controller where the listing create action is residing.

You should permit category_id and subcategory_id in strong params .

Example:

def listing_params
  params.require(:listing).permit(:category_id, :subcategory_id)
end

If you're sure that you're allowing both the parameters, Please share your Rails log.

Don't forget to permit your Params at your controller. Permit both at param.require and It will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM