Reputation: 1415
Im trying to build a Pinteresting clone using Ruby on Rails.
It all works offline but when on heroku i get this error when i click on "New Pin" found here https://fathomless-hamlet-4281.herokuapp.com/pins
I have run heroku run rake:db migrate
When i run heroku logs --tail this is what comes up when i click the button.
Anyone have any clues? Thanks alot.
←[36m2015-02-19T10:29:05.656883+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.656885+00:00 app[web.1]:←[0m
←[33m2015-02-19T10:29:05.953458+00:00 heroku[router]:←[0m at=info method=GET path="/pins/new" host=fathomless-hamlet-4281.herokuapp.com request_id=5259a5f0-869a-41cb-b360-a27c8b59dd0d fwd="101.167.115.4" dyno=web.1 connect=3ms service=10ms status=500 bytes=1754
←[36m2015-02-19T10:29:05.946125+00:00 app[web.1]:←[0m Started GET "/pins/new" for 101.167.115.4 at 2015-02-19 10:29:05 +0000
←[36m2015-02-19T10:29:05.951561+00:00 app[web.1]:←[0m Completed 500 Internal Server Error in 3ms
←[36m2015-02-19T10:29:05.948081+00:00 app[web.1]:←[0m Processing by PinsController#new as HTML
←[36m2015-02-19T10:29:05.952642+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.952645+00:00 app[web.1]:←[0m ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
←[36m2015-02-19T10:29:05.952647+00:00 app[web.1]:←[0m app/controllers/pins_controller.rb:14:in `new'
←[36m2015-02-19T10:29:05.952648+00:00 app[web.1]:←[0m
←[36m2015-02-19T10:29:05.952650+00:00 app[web.1]:←[0m
Here is my pins controller:
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
Here is my views>pins>index.html.erb
<h1>Listing pins</h1>
<table>
<thead>
<tr>
<th>Image</th>
<th>Description</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @pins.each do |pin| %>
<tr>
<td><%= image_tag pin.image.url (:medium) %></td>
<td><%= pin.description %></td>
<td><%= pin.user.email if pin.user %></td>
<td><%= link_to 'Show', pin %></td>
<% if pin.user == current_user %>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if user_signed_in? %>
<%= link_to 'New Pin', new_pin_path %>
<% end %>
<%= link_to 'New Pin', new_pin_path %>
Below is Schema.rb
ActiveRecord::Schema.define(version: 20150210113737) do
create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
add_index "pins", ["user_id"], name: "index_pins_on_user_id"
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Below is 20150205130111_add_user_id_to_pins
class AddUserIdToPins < ActiveRecord::Migration
def change
add_column :pins, :user_id, :interger
add_index :pins, :user_id
end
end
Upvotes: 0
Views: 3903
Reputation: 12340
user_id
is not present ActiveRecord::UnknownAttribute
Error means that you're missing user_id
in the table pins .
create a migration
bundle exec rails generate migration AddUserIdToPins user_id:integer
Then
bundle exec rake db:migrate
Then
heroku restart
if you have already file then please remove the file and commit it to heroku
first
del c:/Users/Joey/desktop/pinteresting/db/migrat e/20150205130111_add_user_id_to_pins.rb
,
Then do
bundle exec rails generate migration AddUserIdToPins user_id:integer
and then
bundle exec rake db:migrate
and
git rm old_migration_file
and
git add new_migration_file
and
git push heroku master
heroku run rake db:migrate
In your controller code please also chnage
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image, :user_id)
end
Upvotes: 0
Reputation: 1468
I guess, lease Restart your server after migration will refresh the schema cache.
If you dont find solution then You're getting UnknownAttributeError because you don't have a column in your pins table called user_id
Upvotes: 1