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

时间:2014-08-20 21:47:38

标签: sql ruby-on-rails ruby

我目前正致力于用户可以创建的功能 列表中保存的列表。因此,当用户想要创建新的列表时,将会有不同类别的下拉列表。我创建了表格和 关系,但我不能让列表保存在 类别。每当我提交列表时,列表表中的category_id列都保持空白,因此列表和类别之间没有连接。有谁知道为什么> 这就是我所拥有的:

模型category.rb

class Category < ActiveRecord::Base
  has_many :listings
end

model listing.rb

class Listing < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  default_scope -> { order('created_at DESC') }
  validates :location, presence: true, length: { maximum: 20 }
  validates :title, presence: true, length: { maximum: 20 }
  validates :user_id, presence: true
  validates :description, presence: true

end

listings_controller

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

categories_controller

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

观看类别/新

<h1>New category</h1>

<%= render 'form' %>

<%= link_to 'Back', categories_path %>

观看类别/ _form.html.erb &LT;

%= form_for(@category) do |f| %>
  <% if @category.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@category.errors.count, "error") %> prohibited
this category from being saved:</h2>

      <ul>
      <% @category.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

观看清单/ 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标题描述位置user_id created_at updated_at category_id

和类别表 id name created_at updated_at

非常感谢

1 个答案:

答案 0 :(得分:1)

将:category_id添加到列表控制器中的listing_params