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
categories.
Here is what I have:
Model 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
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>
      <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 %>
Views Listing/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>
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
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/f7fcc7315842e41f2de3ce28aef53b3b%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.