mezzimench
mezzimench

Reputation: 25

how to add / route advertisements into posts and comments on a web application in the form of sponsored links?

Very new to programming and ruby on rails, working on a CRUD exercise in an app tutorial. The application has posts and comments that are showing properly. The next task is to add native advertising to the posts and comments through sponsored links. Posts and comments are working on the application, but since I created the Advertisement Model I have been experiencing the following routing errors. Thank you for your help:)

In OS X Terminal:

$ rake db:seed rake aborted! ActiveModel::MissingAttributeError: can't write unknown attribute post_id

local.3000 server error:

Started GET "/advertisements" for ::1 at 2015-03-06 20:51:12 -0700 ActionController::RoutingError (uninitialized constant AdvertisementsController):

 ***advertisement_controller.rb

class AdvertisementsController < ApplicationController
  def index
    @advertisements = Advertisement.all
  end

  def show
    @advertisement = Advertisement.find(params[:id])
  end
end

***advertisement.rb

class Advertisement < ActiveRecord::Base

  belongs_to :post
  belongs_to :comment
  
end


***index.html.erb — advertisement

<h1>All Advertisements/h1>
 <% @advertisments.each do |advertisment| %>
  <div class="media">
    <div class="media-body">
      <h4 class="media-heading">
        <%= link_to advertisment.title, advertisment %>
      </h4>
    </div>
  </div>
<%end%>

***show.html.erb — advertisement

<h1><%= @advertisement.title %></h1>
<p><%= @advertisement.copy %></p>

***routes.rb

Rails.application.routes.draw do

  resources :advertisements

  resources :posts

  get 'about' => 'welcome#about'

    root to: 'welcome#index'

***create_advertisements.rb

class CreateAdvertisements < ActiveRecord::Migration
  def change
    create_table :advertisements do |t|
      t.string :title
      t.text :copy
      t.integer :price

      t.timestamps null: false
    end
  end
end

***seeds.rb

require 'faker'

#Create Posts
50.times do 
  Post.create!(
    title: Faker::Lorem.sentence,
    body:   Faker::Lorem.paragraph
    )  
end
posts = Post.all

#Create Comments
100.times do
  Comment.create!(
    post: posts.sample,
    body: Faker::Lorem.paragraph
    )
end

#Create Advertisements
15.times do
  Advertisement.create!(
    post: posts.sample,
    body: Faker::Commerce.product_name,
    title: Faker::Hacker.say_something_smart, 
    copy: Faker::Lorem.sentence(3, true),
    price: Faker::Commerce.price
    )
end

puts "Seed finished"
puts "#{Post.count} posts created"
puts "#{Comment.count} comments created"
puts "#{Advertisement.count} advertisements created"

Upvotes: 1

Views: 199

Answers (1)

vich
vich

Reputation: 11896

Looks like you're missing a few columns in your Advertisements table migration. Aside from the timestamps, you're creating three columns: :title, :copy, and :price; however, in your seeds.rb file you're "faking" Advertisements with two missing columns: :post and :body.

Since Advertisements belong_to Posts and Comments you want to make sure to add post_id and comment_id as integers in your advertisements table.

After you've added those columns, including the :post and :body (if you want them as well), run rake db:migrate and then try rake db:seed.

Upvotes: 1

Related Questions