I'm trying to associate "Notes" with "Account one the same page (show.html.erb) once the note is created. However, once I create the note for the associated account I get undefined local variable or method `accounts' for NotesController:0x0000000545e1d8"
Notes were being created fine up until the point of association.
with @account = accounts.find(params[:account_id]) highlighted on like 65.
**Parameters:** (on debug page)
{"utf8"=>"✓",
"authenticity_token"=>"gm6gJ3mYDDVmDHnsB2JqhOtOY1Lc9n3P7Yuq9gpqdo4=",
"note"=>{"comment"=>"1221211212"},
"commit"=>"Create Note",
"account_id"=>"1"}
Running Rails 4.1.8 and ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
note_colltroler.rb
class NotesController < ApplicationController
before_action :set_note, only: [:edit, :update, :destroy]
before_action :set_account
before_action :authenticate_user!
before_action :check_user, only: [:edit, :update, :destroy]
# GET /notes/new
def new
@note = note.new
end
# GET /notes/1/edit
def edit
end
# POST /notes
# POST /notes.json
def create
@note = note.new(note_params)
@note.user_id = current_user.id
@note.account_id = @account.id
respond_to do |format|
if @note.save
format.html { redirect_to @account, notice: 'note was successfully created.' }
format.json { render :show, status: :created, location: @note }
else
format.html { render :new }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /notes/1
# PATCH/PUT /notes/1.json
def update
respond_to do |format|
if @note.update(note_params)
format.html { redirect_to account_path(@account), notice: 'note was successfully updated.' }
format.json { render :show, status: :ok, location: @note }
else
format.html { render :edit }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note.destroy
respond_to do |format|
format.html { redirect_to account_path(@account), notice: 'note was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_note
@note = note.find(params[:id])
end
def set_account
@account = accounts.find(params[:account_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def note_params
params.require(:note).permit(:comment)
end
end
note.rb
class Note < ActiveRecord::Base
belongs_to :user
belongs_to :account
end
account.rb
class Account < ActiveRecord::Base
belongs_to :program
has_many :notes
end
Rake Routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
account_notes POST /accounts/:account_id/notes(.:format) notes#create
new_account_note GET /accounts/:account_id/notes/new(.:format) notes#new
edit_account_note GET /accounts/:account_id/notes/:id/edit(.:format) notes#edit
account_note PATCH /accounts/:account_id/notes/:id(.:format) notes#update
PUT /accounts/:account_id/notes/:id(.:format) notes#update
DELETE /accounts/:account_id/notes/:id(.:format) notes#destroy
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:id/edit(.:format) accounts#edit
account GET /accounts/:id(.:format) accounts#show
PATCH /accounts/:id(.:format) accounts#update
PUT /accounts/:id(.:format) accounts#update
DELETE /accounts/:id(.:format) accounts#destroy
programs GET /programs(.:format) programs#index
POST /programs(.:format) programs#create
new_program GET /programs/new(.:format) programs#new
edit_program GET /programs/:id/edit(.:format) programs#edit
program GET /programs/:id(.:format) programs#show
PATCH /programs/:id(.:format) programs#update
PUT /programs/:id(.:format) programs#update
DELETE /programs/:id(.:format) programs#destroy
pages_index GET /pages/index(.:format) pages#index
root GET / pages#index
For Notes show.html.erb
<div class="row">
<div class="col-md-3">
<p>
<strong>First name:</strong>
<%= @account.first_name %>
</p>
<p>
<strong>Last name:</strong>
<%= @account.last_name %>
</p>
<p>
<strong>Program:</strong>
<%= @account.program.program %>
</p>
<p>
<strong>Address:</strong>
<%= @account.address %>
</p>
<p>
<strong>Phone:</strong>
<%= @account.phone %>
</p>
<p>
<strong>Created:</strong>
<%= @account.created_at %>
</p>
<%= link_to "Write a Note", new_account_note_path(@account), class: "btn btn-primary" %>
</div>
<div class="col-md-9">
<% @notes.each do |note| %>
<p><%= note.comment %></p>
<% end %>
<%= link_to 'Edit', edit_account_path(@account), class: "btn btn-link" %> |
<%= link_to 'Back', account_path, class: "btn btn-link" %>
</div>
</div>