Reputation: 69
What did I miss?
ERROR: undefined local variable or method `feedback' for #<#<Class:0x007f66dc8dca30>:0x007f66dc8cee80>
Migration:
class CreateFeedbacks < ActiveRecord::Migration
def change
create_table :feedbacks do |t|
t.references :user
t.text :body
t.timestamps
end
end
end
Model:
class Feedback < ActiveRecord::Base
attr_accessible :body, :user
belongs_to :user
end
Controller:
class FeedbacksController < ApplicationController
before_action :authenticate_user!
def index
@feedback = Feedback.all
end
def new
@feedback = Feedback.new
end
def create
@user = User.find(params[:user])
@feedback = @user.feedbacks.create(params[:feedback])
respond_to do |format|
if @feedback.save
format.html { redirect_to @user, notice 'Comment was successfully created.' }
format.json { render json: @feedback, status: :created, location: @feedback }
else
format.html { render action: "new" }
format.json { render json: @feedback.errors, status: :unprocessable_entily }
end
end
end
User model:
has_many :feedback
Routes:
resources :feedbacks
resources :users do
resources :feedbacks
end
Upvotes: 0
Views: 2028
Reputation: 174
Firstly, it helps a lot to style your question and code correctly as well as mark your files (routes.rb
, controllers
, models
correctly). Else it would be very difficult for readers to understand your question and spot issues with your code. You can refer to https://meta.stackoverflow.com/editing-help for the styleguide/ markdown.
Secondly, it looks like your routes.rb
are incorrect - but again, this could be due to your formatting. Based on your question, it looks like your routes are:
resources :feedbacks
resources :users do
resources :feedbacks
end
end
When it should be:
resources: users do
resources: feedbacks
end
Thirdly, your seem to have a typo in your model (feedback
instead of feedbacks
. That is:
class User < ActiveRecord::Base
has_many :feedbacks
end
class Feedback < ActiveRecord::Base
belongs_to :user
end
Lastly, do add specifics and what exactly you ran that caused the error. E.g. did you run something in console, what page did you load?
Upvotes: 1