Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialruby gupta
1,200 Pointscan't write unknown attribute `document_id`
when i was writing the creat table in Create_document.rb by mistake i wrote "add_column :statuses, :documents_id, :integer" and i run the migrate "$ bin/rake db:migrate" which create the table. after that i done the changes "add_column :statuses, :document_id, :integer" and again run the migrate. but my problem was not solve it was giving same error in my rails server
"ActiveModel::MissingAttributeError in StatusesController#new
can't write unknown attribute document_id
Rails.root: C:/Users/Destop/project/Treebook
Application Trace | Framework Trace | Full Trace
app/controllers/statuses_controller.rb:31:in `new' "
What may be causing this error?
i can see my rails sever is running perfectly when i remove "@status.build_document" from "statuses_controller.rb"
bt it's not creatng any attachment. I am totally missing document and i try many time bt i didnt get any solution.
This is my Create_document.rb
class CreateDocuments < ActiveRecord::Migration
def change
create_table :documents do |t|
t.integer :user_id
t.timestamps
end
add_index :documents, :user_id
add_attachment :documents, :attachment
add_column :statuses, :documents_id, :integer
end
end
$ bin/rake db:migrate
DL is deprecated, please use Fiddle
== 20150609042025 CreateDocuments: migrating ==================================
-- create_table(:documents)
-> 0.0280s
-- add_index(:documents, :user_id)
-> 0.0090s
-- add_attachment(:documents, :attachment)
-> 0.0050s
-- add_column(:statuses, :documents_id, :integer)
-> 0.0010s
== 20150609042025 CreateDocuments: migrated (0.0520s) =========================
This is my app/models/document.rb
class Document < ActiveRecord::Base
attr_accessible :attachment
has_attached_file :attachment
end
This is my status.rb:-
class Status< ActiveRecord::Base
attr_accessible :context, :user_id, :document_attributes
belongs_to :user
belongs_to :document
accepts_nested_attributes_for :document
validates :context, presence: true,
length: { minimum: 2}
validates :user_id, presence: true
end
This is my statuses controller.rb
# GET /statuses/new
def new
@status = current_user.statuses.new
@status.build_document
respond_to do |format|
format.html #index.html.erb
format.json { render json: @status }
end
end
# GET /statuses/1/edit
def edit
@status = current_user.statuses.find(params[:id])
end
Thank you very much!!!!!!!!
ruby gupta
1,200 PointsThis is Schema.rb
ActiveRecord::Schema.define(version: 20150609042025) do
create_table "documents", force: :cascade do |t|
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "attachment_file_name"
t.string "attachment_content_type"
t.integer "attachment_file_size"
t.datetime "attachment_updated_at"
end
add_index "documents", ["user_id"], name: "index_documents_on_user_id"
create_table "statuses", force: :cascade do |t|
t.text "context"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "documents_id"
end
add_index "statuses", ["user_id"], name: "index_statuses_on_user_id"
create_table "user_friendships", force: :cascade do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "state"
end
add_index "user_friendships", ["state"], name: "index_user_friendships_on_state"
add_index "user_friendships", ["user_id", "friend_id"], name:
"index_user_friendships_on_user_id_and_friend_id"
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "profile_name"
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
1 Answer
Daniel Cunningham
21,109 PointsGiven your schema and the error, I'm guessing that you have misspelled the column you're trying to access. Your schema shows that your statuses table column is "documents_id". Meanwhile, your error message says "can't write unknown attribute document_id". I'm not sure if there is some code that you haven't posted on the forum, but I'm guessing your build_document method is going to reach out to an attribute of document_id when it needs to go for documents_id instead.
ruby gupta
1,200 PointsThis is my Statuses_controller.rb
class StatusesController < ApplicationController
before_action :set_status, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
# GET /statuses
# GET /statuses.json
def index
@statuses = Status.all
respond_to do |format|
format.html #index.html.erb
format.json { render json: @statuses }
end
end
# GET /statuses/1
# GET /statuses/1.json
def show
@status = Status.find(params[:id])
respond_to do |format|
format.html #index.html.erb
format.json { render json: @status }
end
end
# GET /statuses/new
def new
@status = current_user.statuses.new
@status.build_document
respond_to do |format|
format.html #index.html.erb
format.json { render json: @status }
end
end
# GET /statuses/1/edit
def edit
@status = current_user.statuses.find(params[:id])
end
# POST /statuses
# POST /statuses.json
def create
@status = current_user.statuses.new(params[:status])
respond_to do |format|
if @status.save
format.html { redirect_to @status, notice: 'Status was successfully created.' }
format.json { render json: @status, status: :create, location: @status }
else
format.html { render action: "new" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /statuses/1
# PATCH/PUT /statuses/1.json
def update
@status = current_user.statuses.find(params[:id])
if params[:status] && params[:status].has_key?(:user_id)
params[:status].delete(:user_id)
end
respond_to do |format|
if @status.update_attributes(params[:status])
format.html { redirect_to @status, notice: 'Status was successfully updated.' }
format.json { head :no_context }
else
format.html { render action: "edit" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statuses/1
# DELETE /statuses/1.json
def destroy
@status.destroy
respond_to do |format|
format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
format.json { head :no_context }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_status
@status = Status.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def status_params
params.require(:status).permit(:name, :context, :user_id)
end
end
ruby gupta
1,200 Pointseven i'm writing documents_id i got same error, i really don't know were i have done mistake .can u plz help me.
Daniel Cunningham
21,109 PointsThe very first index you have in the status_controller.rb is @statuses. Everything else is @status. Could that have anything to do with what's happening?
ruby gupta
1,200 PointsWhen i try to change @status.build_document to @statuses.build_document in statuses_controller.rb
it's give me error:-
NoMethodError in StatusesController#new
undefined method `build_document' for nil:NilClass
Daniel Cunningham
21,109 Pointshttp://stackoverflow.com/questions/16527338/activemodelmissingattributeerror-in-rails-4
The url above is a forum post on someone who experienced the same error. It is an issue with associating the request with the columns in your database. Your method is calling a "document_id" column that doesn't exist in your schema (your column is "documents_id"). I'm guessing that renaming the column would probably mess up something else, so I'd look for any associations in your methods to make sure that it seeks the documents_id column. Good luck!
ruby gupta
1,200 PointsThank you so much for ur help, i truly appreciate! your time and effort.
I change" douments_id" to "document_id" in both "Create_document.rb and schema.rb" after that i run bin/rake db:reset and bin/rake db:migrate and now it's work :)
Daniel Cunningham
21,109 PointsDaniel Cunningham
21,109 PointsWhat does your Schema.rb look like?
Also, where in your "Statuses_Controller.rb" file does it reference document_id? What is the ".build_document" method that you are using in your statuses new method?