【问题标题】:Why are listings not saved in category?为什么列表没有保存在类别中?
【发布时间】:2014-10-14 09:59:05
【问题描述】:

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

模型类别.rb

class Category < ActiveRecord::Base
  has_many :listings
end

模型列表.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

%= 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 标题 描述 location user_id created_at updated_at category_id

和类别表 id 名称 created_at updated_at

非常感谢

【问题讨论】:

  • 好的,刚刚发现错误。我没有在 :category_id 到参数

标签: sql ruby-on-rails ruby


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2023-02-18
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多