Sql 为什么列表不保存在类别中?

Sql 为什么列表不保存在类别中?,sql,ruby-on-rails,ruby,Sql,Ruby On Rails,Ruby,我目前正在开发用户可以创建的功能 不保存在类别中的列表。因此,当用户想要创建一个新的列表时,会有一个包含不同类别的下拉列表。我创建了表格和 关系,但我无法获取列表,请将其保存在 类别。每当我提交列表时,listings表中的category_id列将保持空白,因此列表和类别之间没有任何连接。有人知道为什么吗> 以下是我所拥有的: Model category.rb class Category < ActiveRecord::Base has_many :listings end 控

我目前正在开发用户可以创建的功能 不保存在类别中的列表。因此,当用户想要创建一个新的列表时,会有一个包含不同类别的下拉列表。我创建了表格和 关系,但我无法获取列表,请将其保存在 类别。每当我提交列表时,listings表中的category_id列将保持空白,因此列表和类别之间没有任何连接。有人知道为什么吗> 以下是我所拥有的:

Model category.rb

class Category < ActiveRecord::Base
  has_many :listings
end
控制器

def new
      @listing = Listing.new
      @listings = Listing.paginate(page: params[:page])
      @categories = Category.all
    end

def create
    @listing = current_user.listings.build(listing_params)
    @categories = Category.all
    if @listing.save
      flash[:success] = "Job Post created"
      redirect_to current_user
      else
        render 'listings/new'
      end
    end
class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  # GET /categories
  # GET /categories.json
  def index
    @categories = Category.all
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
  end

  # GET /categories/new
  def new
    @category = Category.new
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  # POST /categories.json
  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @category, notice: 'Category was successfully created.' }
        format.json { render :show, status: :created, location: @category }
      else
        format.html { render :new }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:name)
    end
end
class CategoriesController
视图类别/新

<h1>New category</h1>

<%= render 'form' %>

<%= link_to 'Back', categories_path %>
新类别
视图类别/_form.html.erb

禁止 要保存此类别,请执行以下操作:

视图列表/new.html.erb

<div class="top">
<%= form_for(@listing) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

</div>
<h1> Create Job Post </h1>
<body>
<div class=" form row">
  <div class="span6 offset3 ">

    <%= f.label :title %>
    <%= f.text_field :title %>

    <%= f.label :location %>
    <%= f.text_field :location %>
    <br>

      <%= f.text_area :description, placeholder: "Compose new
listing...", class: "description_form" %>
 <br>
  <p><b>Category:</b><br>

  <select name="listing[category_id]">
   <% @categories.each do |category| %>
       <option value="<%= category.id %>"
         <%= ' selected' if category.id == @listing.category_id %>>
         <%= category.name %>
       </option>
   <% end %>
  </select></p>


</div>

<div class="space-button-form">
  <%= f.submit "Post",  class: "tfbutton tfbutton-search
tfbutton-sign-up post" %>
</div>

<% end %>
</div>
</div>
</div>

创造就业岗位


类别:

我有一个包含以下列的列表表: id标题描述位置用户\u id创建于\u更新于\u类别\u id

和类别表 在更新时创建的id名称


非常感谢

将:category_id添加到listings controller中的listing_参数中

好的,刚刚发现错误。我没有在:category_id上找到参数
def new
      @listing = Listing.new
      @listings = Listing.paginate(page: params[:page])
      @categories = Category.all
    end

def create
    @listing = current_user.listings.build(listing_params)
    @categories = Category.all
    if @listing.save
      flash[:success] = "Job Post created"
      redirect_to current_user
      else
        render 'listings/new'
      end
    end
class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  # GET /categories
  # GET /categories.json
  def index
    @categories = Category.all
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
  end

  # GET /categories/new
  def new
    @category = Category.new
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  # POST /categories.json
  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @category, notice: 'Category was successfully created.' }
        format.json { render :show, status: :created, location: @category }
      else
        format.html { render :new }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:name)
    end
end