Couldn't find Resturant with 'id'=5 - ruby-on-rails

I am building this app and nested the reviews inside the restaurants. i have set all the permisions so only the users who are the owner of the review can delete their reviews. when i delete the review , the restaurant also gets deleted and i get this error Couldn't find Restaurant with 'id'=5. How can i solve that issue ? here's my code:
resturants_controller
class ResturantsController < ApplicationController
before_action :set_resturant, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , except: [:index,:show]
before_action :check_user , only: [:edit,:update,:destroy]
# GET /resturants
# GET /resturants.json
def index
#resturants = Resturant.all
end
# GET /resturants/1
# GET /resturants/1.json
def show
#reviews = Review.where(resturant_id: #resturant.id)
if #reviews.blank?
#average_rating = 0
else
#average_rating = #reviews.average(:rating).round(2)
end
end
# GET /resturants/new
def new
#resturant = Resturant.new
end
# GET /resturants/1/edit
def edit
end
# POST /resturants
# POST /resturants.json
def create
#resturant = Resturant.new(resturant_params)
#resturant.user_id = current_user.id
respond_to do |format|
if #resturant.save
format.html { redirect_to #resturant, notice: 'Resturant was successfully created.' }
format.json { render :show, status: :created, location: #resturant }
else
format.html { render :new }
format.json { render json: #resturant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /resturants/1
# PATCH/PUT /resturants/1.json
def update
respond_to do |format|
if #resturant.update(resturant_params)
format.html { redirect_to #resturant, notice: 'Resturant was successfully updated.' }
format.json { render :show, status: :ok, location: #resturant }
else
format.html { render :edit }
format.json { render json: #resturant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /resturants/1
# DELETE /resturants/1.json
def destroy
#resturant.destroy
respond_to do |format|
format.html { redirect_to resturants_url, notice: 'Resturant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_resturant
#resturant = Resturant.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def resturant_params
params.require(:resturant).permit(:name, :descirption, :website, :phone,:image)
end
def check_user
unless #resturant.user = current_user
redirect_to root_path , alert: "Sorry this Resturant belongs to someone else"
end
end
end
reviews_controller
class ReviewsController < ApplicationController
before_action :set_review, only: [:edit, :update, :destroy]
before_action :set_resturant
before_action :authenticate_user!
before_action :check_user, only: [:edit, :update, :destroy]
# GET /reviews/new
def new
#review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.resturant_id = #resturant.id
respond_to do |format|
if #review.save
format.html { redirect_to resturant_path(#resturant), notice: 'Review was successfully created.' }
format.json { render :show, status: :created, location: #review }
else
format.html { render :new }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to #review, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: #review }
else
format.html { render :edit }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to resturant_path(#resturant), notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
#review = Review.find(params[:id])
end
def set_resturant
#resturant = Resturant.find(params[:resturant_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
def check_user
unless #review.user = current_user
redirect_to root_path , alert: "Sorry this review belongs to someone else"
end
end
end
resturants/show.html.erb
<div class="row">
<div class="col-md-5">
<p id="notice"><%= notice %></p>
<%= image_tag #resturant.image_url %>
<div class="star-rating" data-score= <%= #average_rating %> ></div>
<p><%= "#{#reviews.length} reviews"%></p>
<p>
<strong>Name:</strong>
<%= #resturant.name %>
</p>
<p>
<strong>Descirption:</strong>
<%= #resturant.descirption %>
</p>
<p>
<strong>Website:</strong>
<%= link_to #resturant.website, #resturant.website %>
</p>
<p>
<strong>Phone:</strong>
<%= #resturant.phone %>
</p>
<%= link_to "Write a review", new_resturant_review_path(#resturant), class: "btn btn-danger"%>
<%= link_to 'Edit', edit_resturant_path(#resturant) %> |
<%= link_to 'Back', resturants_path %>
</div>
<div class="col-md-7">
<% if #reviews.blank? %>
<h3>No Reviews yet</h3>
<%else%>
<table class="table">
<tbody>
<% #reviews.each do |review|%>
<tr>
<td>
<div class="star-rating" data-score= <%= review.rating%> ></div>
<p><%= review.comment%></p>
<%if user_signed_in?%>
<%if (review.user == current_user)%>
<%= link_to "Edit", edit_resturant_review_path(#resturant,review)%>
<%= link_to "Delete", resturant_review_path(#resturant,review), method: :delete %>
<%end%>
<%end%>
</td>
</tr>
<%end%>
</tbody>
</table>
<%end%>
</div>
<script type="text/javascript">
$('.star-rating').raty({
path: 'https://s3.ap-south-1.amazonaws.com/starsratings',
readOnly: true ,
score: function() {
return $(this).attr('data-score');
}
});
</script>
the Routes file
Rails.application.routes.draw do
get 'pages/welcome'
get 'pages/about'
devise_for :users
root 'resturants#index'
resources :resturants do
resources :reviews , except: [:index,:show]
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
rake routes file
Prefix Verb URI Pattern Controller#Action
pages_welcome GET /pages/welcome(.:format) pages#welcome
pages_about GET /pages/about(.:format) pages#about
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
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
root GET / resturants#index
resturant_reviews POST /resturants/:resturant_id/reviews(.:format) reviews#create
new_resturant_review GET /resturants/:resturant_id/reviews/new(.:format) reviews#new
edit_resturant_review GET /resturants/:resturant_id/reviews/:id/edit(.:format) reviews#edit
resturant_review PATCH /resturants/:resturant_id/reviews/:id(.:format) reviews#update
PUT /resturants/:resturant_id/reviews/:id(.:format) reviews#update
DELETE /resturants/:resturant_id/reviews/:id(.:format) reviews#destroy
resturants GET /resturants(.:format) resturants#index
POST /resturants(.:format) resturants#create
new_resturant GET /resturants/new(.:format) resturants#new
edit_resturant GET /resturants/:id/edit(.:format) resturants#edit
resturant GET /resturants/:id(.:format) resturants#show
PATCH /resturants/:id(.:format) resturants#update
PUT /resturants/:id(.:format) resturants#update
DELETE /resturants/:id(.:format) resturants#destroy
models/review.rb
class Review < ApplicationRecord
belongs_to :user , dependent: :destroy
belongs_to :resturant , dependent: :destroy
end
**models/resturant.rb**
class Resturant < ApplicationRecord
mount_uploader :image, ImageUploader
belongs_to :user, dependent: :destroy
validates :name , :descirption , :website , :phone , presence: true
has_many :reviews
validates :phone , numericality: {
only_integer: true,
}
end
my github repo

When you delete a review then the restaurant gets deleted too because of this line:
belongs_to :resturant , dependent: :destroy
Change it to:
belongs_to :resturant
And you have a similar line in Resturant that means: when a resturant is deleted then delete the resturant's user too. You probably do not want to do that.
belongs_to :user, dependent: :destroy
The dependent option does not tell what should happen to self when the association is deleted, but what should happen to associated objects when self is deleted.
IMHO the following configuration would make more sense:
class Resturant < ApplicationRecord
belongs_to :user
has_many :reviews, dependent: :destroy
class Review < ApplicationRecord
belongs_to :resturant
belongs_to :user
class User < ApplicationRecord
has_many :resturants, dependent: :destroy
has_many :reviews, dependent: :destroy

Related

uninitialized constant RsvpsController

so i'm getting this error when I press my rsvp button from my ui. Maybe theres something wrong with my route or something wrong with my controller.
I'm not too sure at this point. I'm quite new to rails and I've been stuck on this issue for far too long. When I do make some changes another error pops up. I would truly appreciate some help
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvp.build({post_id: params[:id]})
if rsvp.save
end
end
end
post controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find(params[:id])
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
unless current_user == #post.user
redirect_back fallback_location: root_path, notice: 'User is not owner'
end
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :user_id, :description, :name, :address)
end
load_and_authorize_resource
def create
#rsvp=rsvp.new
end
end
=
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
devise_for :users
root 'home#index'
get 'home/ruby_meetup'
resources :posts do
post 'rsvp', to: 'rsvps#create', on: :member
end
Also I want the number of users who rsvped/registered to show up but I'm getting random numbers and letters showing up on my ui. Is there something not right with my show page?
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User_id:</strong>
<%= #post.user_id %>
</p>
<p><strong>Address:</strong> <%= #post.address %></p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<p>
<strong>registered:</strong>
<%=#post.user %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<div class="rsvp"><%= button_to "Rsvp now", rsvp_post_path(#post), class: "btn btn-primary" %></div>
<div class="map"><%= image_tag "http://maps.googleapis.com/maps/api/staticmap?center=#{#post.latitude},#{#post.longitude}&markers=#{#post.latitude},#{#post.longitude}&zoom=12&size=450x400&key=AIzaSyCKzKMEhSNgwSXf7WV71pHWgzdpMkPn8W4",
class: 'img-fluid img-rounded', alt: "#{#post} on the map"%>
</div>
controller class name should be RsvpsController currently its RsvpController, note that s is missing after Rsvp.
Change your route to this:
post 'rsvp', to: 'rsvp#create', on: :member

ActiveRecord::RecordNotFound in PermitsController#detail, Couldn't find Permit with 'id'=

Hi guys I keep get this error when i try to access to render out one specific permit data from the permits model to the detail.html.erb, i search through all my code and couldn't find the bug. Can you guys help me to check which part i did wrong? Btw I'm implementing a website using Ruby on rails
This is my permits_controller.rb
class PermitsController < ApplicationController
before_action :set_permit, only: [:destroy]
def index
#permits = Permit.where(:user_id => current_user.id)
end
def new
#permits = Permit.new
end
def create
#permits = current_user.permits.build(permit_params)
if #permits.save
redirect_to invoice_path
else
render 'new'
end
end
def destroy
Permit.destroy_all(user_id: current_user)
respond_to do |format|
format.html { redirect_to root_path, notice: 'Permit was successfully canceled.' }
format.json { head :no_content }
end
end
def confirm
#fields = %i[vehicle_type, carplate, studentid, name, department, permitstart, permitend]
#permit = current_user.permits.build(permit_params)
render :new and return unless #permit.valid?
end
def show
#permits = Permit.where(:user_id => current_user.id)
end
def update
#permits = Permit.where(user_id: current_user).take
respond_to do |format|
if #permits.update(permit_params)
format.html { redirect_to root_path}
flash[:success] = "Permit successfully updated"
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def edit
#permits = Permit.find(params[:id])
##permits = Permit.find_or_initialize_by(user_id: params[:id])
end
def detail
#permits = Permit.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
end
end
Detail.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2><%= #permits.permitstart %></h2>
</div>
</div>
Route.db
Rails.application.routes.draw do
resources :users
resources :permits do
collection do
post :confirm
end
end
resources :visitor_permits
root 'static_pages#home'
get 'viewpermit' =>'permits#detail'
get 'invoice' => 'permits#invoice'
get 'payment' =>'transaction#new'
get 'show_visitor_permit' =>'visitor_permits#show'
get 'show_permit' =>'permits#show'
get 'visitorpermit' => 'visitor_permits#new'
post 'createpermit' => 'permits#create'
get 'homepage/index'
post 'permits' => 'permits#create'
get 'permitapplication' => 'permits#new'
get 'adminlogin' => 'admin_controller#index'
get 'patrollogin' => 'patrol_officer_controller#index'
get 'createcitation' => 'citations#new'
get 'contact'=> 'static_pages#contact'
get 'about' => 'static_pages#about'
get 'signup' => 'users#new'
get 'help' => 'static_pages#help'
post 'users' => 'users#create'
get 'login' => 'sessions#new' #Page for a new session
post 'login' => 'sessions#create' #Create a new session
delete 'logout'=>'sessions#destroy' #Delete a session
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
This is my linking page (show.html.erb)
<h2>My Permits</h2>
<table>
<tr>
<th>Permit Start Date</th>
<th>Permit End Data</th>
<th>Action</th>
<th> </th>
</tr>
<% #permits.each do |permit| %>
<tr>
<td><%= permit.permitstart %></td>
<td><%= permit.permitend %></td>
<td><%= link_to 'Detail', viewpermit_path(permit) %></td>
<td><%= link_to 'Edit', edit_permit_path(permit) %></td>
</tr>
<% end %>
</table>
You haven't defined your routes in a proper way. Your routes simply redirects to action #detail but you don't set your variables within the route. You have multiple options
resources :permits do
collection do
post :confirm
end
member do
get 'viewpermit' # enables a route like /permits/1/viewpermit
end
end
# or
get 'viewpermit/:id' => 'permits#detail' #enables route /viewpermit/1
EDIT:
In your show action.replace where to find
def show
#permits = Permit.find(params[:id])
end

RoR4: NoMethodError in nested resources' view

I have a parent/children relationship model with nested resources setup in order to create child data under a certain parent from it's show page. However, when I click on the child's new page link from the parent page ("http://localhost:3000/brands/1/restaurants/new"), I get an error saying that "NoMethodError in...". Please help.
config/routes.rb
Rails.application.routes.draw do
resources :brands, shallow: true do
resources :restaurants
end
resources :managers
# Prefix Verb URI Pattern Controller#Action
# brand_restaurants GET /brands/:brand_id/restaurants(.:format) restaurants#index
# POST /brands/:brand_id/restaurants(.:format) restaurants#create
# new_brand_restaurant GET /brands/:brand_id/restaurants/new(.:format) restaurants#new
# edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
# restaurant GET /restaurants/:id(.:format) restaurants#show
# PATCH /restaurants/:id(.:format) restaurants#update
# PUT /restaurants/:id(.:format) restaurants#update
# DELETE /restaurants/:id(.:format) restaurants#destroy
# brands GET /brands(.:format) brands#index
# POST /brands(.:format) brands#create
# new_brand GET /brands/new(.:format) brands#new
# edit_brand GET /brands/:id/edit(.:format) brands#edit
# brand GET /brands/:id(.:format) brands#show
# PATCH /brands/:id(.:format) brands#update
# PUT /brands/:id(.:format) brands#update
# DELETE /brands/:id(.:format) brands#destroy
# managers GET /managers(.:format) managers#index
# POST /managers(.:format) managers#create
# new_manager GET /managers/new(.:format) managers#new
# edit_manager GET /managers/:id/edit(.:format) managers#edit
# manager GET /managers/:id(.:format) managers#show
# PATCH /managers/:id(.:format) managers#update
# PUT /managers/:id(.:format) managers#update
# DELETE /managers/:id(.:format) managers#destroy managers#destroy
end
models/brand.rb
class Brand < ActiveRecord::Base
has_many :restaurants
end
models/restaurants.rb
class Restaurant < ActiveRecord::Base
belongs_to :brand
end
controllers/restaurants_controller.rb
class RestaurantsController < ApplicationController
before_action :set_restaurant, only: [:new, :create, :show, :edit, :update, :destroy]
# GET /restaurants
# GET /restaurants.json
def index
#restaurants = Restaurant.all
end
# GET /restaurants/1
# GET /restaurants/1.json
def show
end
# GET /restaurants/new
def new
# #restaurant = Restaurant.new
#restaurant = Restaurant.new
end
# GET /restaurants/1/edit
def edit
end
# POST /restaurants
# POST /restaurants.json
def create
#restaurant = Restaurant.new(restaurant_params)
respond_to do |format|
if #restaurant.save
format.html { redirect_to brands_path, notice: 'Restaurant was successfully created.' }
format.json { render :show, status: :created, location: #restaurant }
else
format.html { render :new }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /restaurants/1
# PATCH/PUT /restaurants/1.json
def update
respond_to do |format|
if #restaurant.update(restaurant_params)
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully updated.' }
format.json { render :show, status: :ok, location: #restaurant }
else
format.html { render :edit }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /restaurants/1
# DELETE /restaurants/1.json
def destroy
#restaurant.destroy
respond_to do |format|
format.html { redirect_to restaurants_url, notice: 'Restaurant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_restaurant
#restaurant = Restaurant.find_by(params[:brand_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def restaurant_params
params.require(:restaurant).permit(:name, :brand_id) if params[:restaurant]
end
end
views/brands/show.html
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #brand.name %>
</p>
<p>
<strong>Manager:</strong>
<%= #brand.manager_id %>
</p>
<table>
<% #brand.restaurants.each do |restaurant| %>
<tr>
<td><%= restaurant.name %></td>
<td><%= restaurant.brand_id %></td>
<td><%= link_to 'Show', restaurant %></td>
<td><%= link_to 'Edit', edit_restaurant_path(restaurant) %></td>
<td><%= link_to 'Destroy', restaurant, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br>
**<%= link_to 'Add New Restaurant', new_brand_restaurant_path(#brand) %>**
<%= link_to 'Edit', edit_brand_path(#brand) %> |
<%= link_to 'Back', brands_path %>
What did I do wrong?
Appreciate your help.
Addin Full Error Message:
NoMethodError in Restaurants#new Showing
/Users/kaku/sample/relation/app/views/restaurants/_form.html.erb where
line #1 raised:
undefined method `restaurants_path' for
<#:0x007fd4ec2e46e0> Extracted source (around line #1): 1 2 3 4 5 6
<%= form_for(#restaurant) do |f| %>
<% if #restaurant.errors.any? %>
<%= pluralize(#restaurant.errors.count, "error") %> prohibited this restaurant from being saved:
Trace of template inclusion: app/views/restaurants/new.html.erb
Rails.root: /Users/kaku/sample/relation
Application Trace | Framework Trace | Full Trace
app/views/restaurants/_form.html.erb:1:in
_app_views_restaurants__form_html_erb__1732550980462972679_70276222510780' app/views/restaurants/new.html.erb:3:in
_app_views_restaurants_new_html_erb___209183791776779444_70276222559000'
Request
Parameters:
{"brand_id"=>"2"} Toggle session dump
_csrf_token: "MG3xNH+mscxxPiQqLVkF011BDb5RGlWJPuTN3GiEcGo=" session_id: "e1f92f37d231a058162af62bae976ff5" Toggle env dump
GATEWAY_INTERFACE: "CGI/1.1" HTTP_ACCEPT:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8"
HTTP_ACCEPT_ENCODING: "gzip, deflate, sdch" HTTP_ACCEPT_LANGUAGE:
"ja,en-US;q=0.8,en;q=0.6" REMOTE_ADDR: "::1" REMOTE_HOST: "::1"
SERVER_NAME: "localhost" SERVER_PROTOCOL: "HTTP/1.1" Response
Headers:
None
As you pointed in the code, I believe the error is in this line
<%= link_to 'Add New Restaurant', new_brand_restaurant_path(#brand) %>
You should have #brand in the new method of restaurants_controller
def new
#brand = Brand.find(params[:brand_id])
#restaurant = Restaurant.new
end
And also I noticed that you have defined set_restaurant wrongly. It should be
def set_restaurant
#restaurant = Restaurant.find(params[:id])
end
Update:
undefined method `restaurants_path'
You have nested resources, so this line
<%= form_for(#restaurant) do |f| %>
should be
<%= form_for [#brand, #restaurant] do |f| %>

undefined local variable or method `accounts' for #<NotesController:0x00000004845068>

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>
Instead of
#account = accounts.find(params[:account_id])
try
#account = Account.find(params[:account_id])
Learn more about .find and other query methods here. Most of them are called on an
Side Note:
As #apneadiving pointed out, you generally want to use some sort of authorisation. I don't think you want users to be able to edit accounts of other users. With your current implementation this is possible by simply experimenting in with the account and note ids in the url (e.g. by getting /accounts/1/notes/2/edit any user can edit note 2 of account 1).
Consider scoping records for the current user. One approach is described here.
in
def set_account
#account = accounts.find(params[:account_id])
end
accounts is not defined

Rails 4 Nested Resources/Routes... almost there...?

I have three models - users, manufacturers, and lines. I need users to have multiple manufacturers, and each manufacturer to have multiple lines. I'm to the point where lines is finally pulling the manufacturer_id on the _form, but now it's not saving the 'name' of the 'line', only the id. It'll save the 'name' as nil, like it's not passing in the name from the form. I've read the standard docs on this and I can't for the life of me get it working. It keeps hanging up on the final create stage in the controller, and I have a feeling that I've got a lot of things wrong in the paths of the form and the controller. Hoping someone will take it easy on me and help me get the whole shebang working.
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :manufacturers
end
manufacturer.rb
class Manufacturer < ActiveRecord::Base
belongs_to :user
has_many :lines
end
line.rb
class Line < ActiveRecord::Base
belongs_to :manufacturer
end
lines_controller.rb
class LinesController < ApplicationController
before_action :set_line, only: [:show, :edit, :update, :destroy]
before_filter :load_manufacturer
# GET /lines
# GET /lines.json
def index
#lines = Line.all
end
# GET /lines/1
# GET /lines/1.json
def show
end
# GET /lines/new
def new
#manufacturer = Manufacturer.find(params[:manufacturer_id])
#line = #manufacturer.lines.build
end
# GET /lines/1/edit
def edit
end
# POST /lines
# POST /lines.json
def create
#line = #manufacturer.lines.build
respond_to do |format|
if #line.save
format.html { redirect_to #line, notice: 'Line was successfully created.' }
format.json { render action: 'show', status: :created, location: #line }
else
format.html { render action: 'new' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /lines/1
# PATCH/PUT /lines/1.json
def update
respond_to do |format|
if #line.update(line_params)
format.html { redirect_to #line, notice: 'Line was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lines/1
# DELETE /lines/1.json
def destroy
#line.destroy
respond_to do |format|
format.html { redirect_to lines_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line
#line = Line.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_params
params.require(:line).permit(:name, :manufacturer_id)
end
def load_manufacturer
#manufacturer = Manufacturer.find(params[:manufacturer_id])
end
end
lines/_form.html.erb
<%= form_for [#manufacturer,#line] do |f| %>
<% if #line.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#line.errors.count, "error") %> prohibited this line from being saved:</h2>
<ul>
<% #line.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :manufacturer_id %><br>
<%= f.text_field :manufacturer_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
Linecarder::Application.routes.draw do
resources :manufacturers do
resources :lines
end
devise_for :users
root "pages#home"
get "about" => "pages#about"
match 'users/:id' => 'users#show', :via => "get"
match '/users', :to => 'users#index', :as => "all_users", :via => "get"
match '/users/:name' => 'users#show', via: :get, as: :public_profile
end
rake routes
Prefix Verb URI Pattern Controller#Action
manufacturer_lines GET /manufacturers/:manufacturer_id/lines(.:format) lines#index
POST /manufacturers/:manufacturer_id/lines(.:format) lines#create
new_manufacturer_line GET /manufacturers/:manufacturer_id/lines/new(.:format) lines#new
edit_manufacturer_line GET /manufacturers/:manufacturer_id/lines/:id/edit(.:format) lines#edit
manufacturer_line GET /manufacturers/:manufacturer_id/lines/:id(.:format) lines#show
PATCH /manufacturers/:manufacturer_id/lines/:id(.:format) lines#update
PUT /manufacturers/:manufacturer_id/lines/:id(.:format) lines#update
DELETE /manufacturers/:manufacturer_id/lines/:id(.:format) lines#destroy
manufacturers GET /manufacturers(.:format) manufacturers#index
POST /manufacturers(.:format) manufacturers#create
new_manufacturer GET /manufacturers/new(.:format) manufacturers#new
edit_manufacturer GET /manufacturers/:id/edit(.:format) manufacturers#edit
manufacturer GET /manufacturers/:id(.:format) manufacturers#show
PATCH /manufacturers/:id(.:format) manufacturers#update
PUT /manufacturers/:id(.:format) manufacturers#update
DELETE /manufacturers/:id(.:format) manufacturers#destroy
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
root GET / pages#home
about GET /about(.:format) pages#about
GET /users/:id(.:format) users#show
all_users GET /users(.:format) users#index
public_profile GET /users/:name(.:format) users#show
Update the create action as below:
def create
#line = #manufacturer.lines.build(line_params) ## Pass the params via line_params
respond_to do |format|
if #line.save
format.html { redirect_to #line, notice: 'Line was successfully created.' }
format.json { render action: 'show', status: :created, location: #line }
else
format.html { render action: 'new' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
You are not passing the params that you received from the form. Hence, name of line is not getting saved in database.
Also, in order to permit the attributes for mass-assignment(strong parameters) pass them via line_params method.
UPDATE for OP's question in the comment to resolve
undefined method 'line_url'... for 'format.html { redirect_to #line, notice: 'Line was successfully created.' }'
Use this
format.html { redirect_to manufacturer_line_path(#manufacturer, #line), notice: 'Line was successfully created.' }
instead of
format.html { redirect_to #line, notice: 'Line was successfully created.' }

Resources