jeudi 22 septembre 2016

I'm Getting error "unknown attribute 'user_id' for Assignment"

I have researched this error but couldn't find a answer that works for me.

The Error

ActiveRecord::UnknownAttributeError in AssignmentsController#new

 def new
     @assignment = current_user.assignments.build
   end  

   def create


Controller - assignments_controller.rb :

    class AssignmentsController < ApplicationController
  before_action :authenticate_user!
  before_filter :admin_access, only: [:new, :create, :edit, :update, :destroy]


    def index
     @assignments = Assignment.all
    end


   def show
     @assignment = Assignment.find(params[:id])
   end

   def new
     @assignment = current_user.assignments.build
   end  

   def create
     @assignment = current_user.assignments.build
     @assignment.name = params[:assignment][:name]
     @assignment.description = params[:assignment][:description]
     @assignment.picture = params[:assignment][:picture]

     @assignment.public = params[:assignment][:public]

     if @assignment.save
       flash[:notice] = "Assignment was saved successfully."
       redirect_to @assignment
     else
       flash.now[:alert] = "Error creating assignment. Please try again."
       render :new
     end
   end

     def edit
     @assignment = Assignment.find(params[:id])
     end

      def update
     @assignment = Assignment.find(params[:id])

     @assignment.name = params[:assignment][:name]
     @assignment.description = params[:assignment][:description]
     @assignment.picture = params[:assignment][:picture]

     @assignment.public = params[:assignment][:public]

     if @assignment.save
        flash[:notice] = "Assignment was updated successfully."
       redirect_to @assignment
     else
       flash.now[:alert] = "Error saving assignment. Please try again."
       render :edit
     end
      end

   def destroy
     @assignment = Assignment.find(params[:id])

     if @assignment.destroy
       flash[:notice] = "\"#{@assignment.name}\" was deleted successfully."
       redirect_to action: :index
     else
       flash.now[:alert] = "There was an error deleting the assignment."
       render :show
     end
   end

     private 

     def assignment_params
         params.require(:assignment).permit(:name, :description, :picture)
     end
end

Model - user.rb :

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
    validates_uniqueness_of :username 
  has_many :articles
  has_many :submits
  has_many :comments 
  has_many :assignments

end

Model - assignment.rb :

class Assignment < ActiveRecord::Base
        has_many :submits, dependent: :destroy
        belongs_to :user
end

views

index.html.erb:

  <div class="col-lg-12 text-center">
<h1>Assignments 

    <% if current_user.admin %>
     <%= link_to "New Assignment", new_assignment_path, class: 'btn btn-success' %>
     <% end %>

</h1>
  </div>
        <hr>

 <div class="row">
   <div class="col-md-8">
 <!-- #7 -->
     <% @assignments.each do |assignment| %>
       <div class="media">
         <div class="media-body">
           <h4 class="media-heading">
 <!-- #8 <-->   </br>
             <%= link_to assignment.name, assignment %>
           </h4>
           <small>
             <%= assignment.description %>
           </small>
         </div>
       </div>
     <% end %>
   </div>
   <br>
   <p div class="col-md-4">

   </div>

new.html.erb:

<h1>New Assignment</h1>

 <div class="row">
   <div class="col-md-4">
     <p>Guidelines for topics:</p>
     <ul>
       <li>Make sure the topic is appropriate.</li>
       <li>Never insult dogs.</li>
       <li>Smile when you type.</li>
     </ul>
   </div>
   <div class="col-md-8">
     <%= form_for @assignment do |f| %>
       <div class="form-group">
         <%= f.label :name %>
         <%= f.text_field :name, class: 'form-control', placeholder: "Enter assignment name" %>
       </div>
       <div class="form-group">
         <%= f.label :description %>
         <%= f.text_area :description, rows: 8, class: 'form-control', placeholder: "Enter assignment description" %>
       </div>
                <div class="form-group">
      <%= f.label :picture %>
      <%= f.file_field :picture %>
               </div>
      <br />
       <%= f.submit "Save", class: 'button' %>
     <% end %>
   </div>
 </div>

show.html.erb:

<h1><%= @assignment.name %>

 <% if current_user.admin %>
    <%= link_to "Edit Assignment", edit_assignment_path, class: 'btn btn-success' %>
    <%= link_to "Delete Assignment", @assignment, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this assignment?' } %>
<% end %>
 </h1>
 <div class="row">
   <div class="col-md-8">
     <p class="lead"><%= @assignment.description %></p>
 <!-- #10 -->
     <% @assignment.submits.each do |submit| %>
       <div class="media">
         <div class="media-body">
           <h4 class="media-heading">
             <%= link_to submit.name, submit %>
           </h4>
         </div>
       </div>
     <% end %>
   </div>


   <div id="img">
    <%= image_tag @assignment.picture.url %>
  </div>

  </br>
   <div class="col-md-4">
     <%= link_to "Submit Assignment", new_submit_path(@assignment), class: 'button' %>
   </div>
 </div>

edit.html.erb:

<h1>Edit Assignment</h1>

 <div class="row">
   <div class="col-md-4">
     <p>Guidelines for topics:</p>
     <ul>
       <li>Make sure the topic is appropriate.</li>
       <li>Never insult dogs.</li>
       <li>Smile when you type.</li>
     </ul>
   </div>
   <div class="col-md-8">
     <%= form_for @assignment do |f| %>
       <div class="form-group">
         <%= f.label :name %>
         <%= f.text_field :name, class: 'form-control', placeholder: "Enter assignment name" %>
       </div>
       <div class="form-group">
         <%= f.label :description %>
         <%= f.text_area :description, rows: 8, class: 'form-control', placeholder: "Enter assignment description" %>
       </div>
                <div class="form-group">
      <%= f.label :picture %>
      <%= f.file_field :picture %>
               </div>
      <br />
       <%= f.submit "Save", class: 'button' %>
     <% end %>
   </div>
 </div>


schema.rb:

ActiveRecord::Schema.define(version: 20160922102720) do

  create_table "articles", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
  end

  create_table "assignments", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "comments", force: :cascade do |t|
    t.text     "body"
    t.integer  "user_id"
    t.integer  "article_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "comments", ["article_id"], name: "index_comments_on_article_id"
  add_index "comments", ["user_id"], name: "index_comments_on_user_id"

  create_table "contacts", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.text     "message"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "submits", force: :cascade do |t|
    t.string   "name"
    t.string   "attachment"
    t.integer  "assignment_id"
    t.integer  "user_id"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "username",               default: "",    null: false
    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.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.datetime "created_at",                             null: false
    t.datetime "updated_at",                             null: false
    t.boolean  "admin",                  default: false
    t.string   "firstname"
    t.string   "lastname"
  end

  add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
  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

Aucun commentaire:

Enregistrer un commentaire