Forum: Ruby on Rails Why are listings not saved in category?

5f8123548ad5cb3ca03e5f6e5ea33d6e?d=identicon&s=25 Friederike Geiken (ike)
on 2014-08-19 23:10
Hello everyone,

I'm currently working on the functionality that users can create
listings that are than saved in categories. I created tables and
relationships but I cannot get the listings do be saved in the
Here is what I have:

Model category.rb

class Category < ActiveRecord::Base
  has_many :listings

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


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

def create
    @listing =
    @categories = Category.all
      flash[:success] = "Job Post created"
      redirect_to current_user
        render 'listings/new'

Views categories/new
<h1>New category</h1>

<%= render 'form' %>

<%= link_to 'Back', categories_path %>

Views categories/_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>

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

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

Views Listing/new.html.erb

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

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

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

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

      <%= f.text_area :description, placeholder: "Compose new
listing...", class: "description_form" %>

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


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

<% end %>

I have a listings table with the following columns:
id title description location user_id created_at updated_at category_id

and categories table
id name created_at updated_at

When I create a new listing and than check the listings table, the
category_id field is blank.
Does anyone know why my listings are not saved in the categories?

Thank you very much
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.