I'm using nested routes,I have :booking table and :hotel table
In my routes.rb
resources :hotels do
resources :bookings
end
In my hotel show.html. erb
<%= link_to 'Book Now', new_booking_path(:hotel_id => #hotel.id) %>
In my bookings_controller.rb
def new
#booking = #hotel.bookings.build
end
When the link is clicked, it generates a link
localhost:3000/bookings/new?hotel_id=4
But I still get an error
NoMethodError
Undefined method 'bookings' for nil:NilClass
Extracted source from
def new
#booking = #hotel.bookings.build
end
It appears that you're not retrieving a Hotel object in your new method for your controller. So when you call bookings on the #hotel variable, the nil exception is thrown. It should look something like:
bookings_controller.rb:
def new
#hotel = Hotel.find(params[:hotel_id])
#booking = #hotel.bookings.build
end
Hope it helps!
I think you made a mistake to find #hotel here, Please do this -
In bookings_controller.rb -
def new
#hotel = Hotel.find(params[:hotel_id])
if #hotel.present?
#booking = #hotel.bookings.build
render :new
else
redirect_to root_path
end
end
Related
**I made a controller and model for post and not able to show my post's
title on the show page but repeatedly i am getting the same error **
my posts_controller code :
class PostsController < ApplicationController
def index
end
def new
end
def create
#render plain: params[:post][:body].inspect
#post = Post.new(post_params)
if #post.save
redirect_to posts_path
else
render "new"
end
end
def show
#post= Post.find(:id=>params[:id])
# #article = "prateek"
end
private
def post_params
params.require(:post).permit(:title,:body)
end
end
my show.html.erb file :
<h1><%= #post.title %></h1>
my post.rb file:
class Post < ApplicationRecord
end
**I expect the result but I didn't get anything right
error = NoMethodError in Posts#show
undefined method `title' for nil:NilClass**
You're passing an hash to find, while you're suppose to pass just the id.
#post= Post.find(params[:id])
or use find_by if you want to pass an hash
#post= Post.find_by(:id => params[:id])
I´m new to Rails and can´t find any solution to my problem with my namespace I created.
The problem is that if I like to show all my rooms under localhost:3000/manage/ it does not work.
error: undefined method `title' for nil:NilClass.
If I try to show all my rooms under ../manage/rooms/ it is still not showing all rooms I created error: can´t find id... if I use an id e.g. ../manage/rooms/38 it is working. Hope someone can help me.
So here we go:
I created a namespace like:
routes.rb:
resources :manage
namespace :manage do
resources :rooms
end
Rake routes:
manage_index GET /manage(.:format) manage#index
POST /manage(.:format) manage#create
new_manage GET /manage/new(.:format) manage#new
edit_manage GET /manage/:id/edit(.:format) manage#edit
manage GET /manage/:id(.:format) manage#show
PATCH /manage/:id(.:format) manage#update
PUT /manage/:id(.:format) manage#update
DELETE /manage/:id(.:format) manage#destroy
manage_rooms GET /manage/rooms(.:format) manage/rooms#index
POST /manage/rooms(.:format) manage/rooms#create
new_manage_room GET /manage/rooms/new(.:format) manage/rooms#new
edit_manage_room GET /manage/rooms/:id/edit(.:format) manage/rooms#edit
manage_room GET /manage/rooms/:id(.:format) manage/rooms#show
PATCH /manage/rooms/:id(.:format) manage/rooms#update
PUT /manage/rooms/:id(.:format) manage/rooms#update
DELETE /manage/rooms/:id(.:format) manage/rooms#destroy
a controller under ../manage/rooms_controller.rb
class Manage::RoomsController < ApplicationController
def index
#rooms = Room.all
end
def new
#room = Room.new
end
def create
#ender text: params[:rooms].inspect
#render text: Rails.logger.info(params[:rooms].inspect)
#room = Room.new(room_params)
if #room.save
redirect_to [:manage, #room]
else
render 'new'
end
end
def show
#room = Room.find(params[:id])
end
def edit
#room = Room.find(params[:id])
end
def update
#room = Room.find(params[:id])
if #room.update(params[:rooms].permit(:number_of_rooms, :occupancy, :room_type, :gender, :title))
redirect_to [:manage, #room]
else
render 'edit'
end
end
def destroy
#room = Room.find(params[:id])
#room.destroy
redirect_to manage_index_path
end
private
def room_params
params.require(:rooms).permit(:number_of_rooms, :occupancy, :room_type, :gender, :title)
end
end
and under ... manage/rooms/_index.html.erb it is rendered in /layouts/_navigation_left.html.erb
Thanks! If you need more information just ask.
Thanks sorry I´m quite new to Rails. How do I change the controller / routes so I can manage not just #rooms but even #foo, #fee ...? Thanks.
I try to use:
I do like to show <%= #room.title %> <%= #room.number_of_rooms %>or do you need something else? Sorry!
and <%= render 'manage/rooms/index'%>
I have two models. Hotel model:
class Hotel < ActiveRecord::Base
attr_accessible ...
belongs_to :user
end
User model:
class User < ActiveRecord::Base
devise ...
has_many :hotels
end
hotels_controller.rb
class HotelsController < ApplicationController
def index
#hotels = current_user.hotels
end
def show
#hotel = Hotel.find(params[:id])
end
def new
#hotel = Hotel.new
end
def create
#hotel = Hotel.new(params[:hotel])
#hotel.user = current_user
if #hotel.save
redirect_to hotels_path, notice: "Nice, you added new hotel " + #hotel.title
else
render "new"
end
end
def edit
#hotel = Hotel.find(params[:id])
end
def update
#hotel = Hotel.find(params[:id])
if #hotel.update_attributes(params[:hotel])
redirect_to hotels_path, notice: "Hotel " + #hotel.title + " was successfully updated"
else
render "edit"
end
end
def destroy
#hotel = Hotel.find(params[:id])
#hotel.destroy
redirect_to hotels_path, notice: "Hotel " + #hotel.title + " was deleted"
end
end
When I signed in I'm creating hotel with some fields, after submit it's rederect me to a list of hotels and it's nice. But when I try to delete some of the hotels I get
NoMethodError in HotelsController#index
undefined method `hotels' for nil:NilClass
and after that when go to the home page(root) my session is over and user is logouted. But! Hotel was successfully destroyed. Something with index action... What i do wrong? Have any ideas?
Just add in your layouts/application.html.erb this line in a head block
<%= csrf_meta_tags %>
The problem is that current_user is nil and failing when you try to get hotels on it.
current_user.hotels
So the question is: what sets up current_user?
You're using devise - can you check what you're doing on hotels-index? do you get automatically logged in for that or does it assume you're sometimes logged-in and sometimes not and it's failing to pick that up?
I'm getting a NoMethodError in the new action of my business_controller.
It seems to be acessing the #business object for a form in my view and the error occurs then:
undefined method `businesses_path' for
Here is my new method:
def new
if Business.where(:user_id => current_user.id).first.blank?
#business = Business.new
else
redirect_to user_businesses_path(current_user.id)
end
end
My routes are:
resources :users do
resources :businesses
member do
get 'account'
post 'payment'
put 'update_password'
get 'membership'
end
end
mind.blank's suggested changes
before_filter :check_if_user_has_business, :only => :index
def new
#business = Business.new
end
private
def check_if_user_has_business
redirect_to new_user_business_path(current_user) unless current_user.business
end
Do you have a route businesses_path or only user_businesses_path? If you only have the second one then you should specify that URL in your form:
<%= form_for #business, url: user_businesses_path do |f| %>
Also, if you have the correct associations set up, then you can write your if statement as follows:
if current_user.business.nil? # since it's a has_one association
Here's how I would write it:
Class BusinessesController < ApplicationController
before_filter :check_if_user_has_business, only: :new
def new
#business = Business.new
end
private
def check_if_user_has_business
redirect_to user_businesses_path(current_user) if current_user.business
end
end
My application has two associated models: Magazine and Article:
class Magazine < ActiveRecord::Base
has_one :article
end
class Article < ActiveRecord::Base
belongs_to :magazine
validation_presence_of :title
end
From the Magazine show page I can create a new Article, so my routes.rb is configured like:
resources :magazines, :shallow => true do
resources :articles
end
and in the Magazine show page I have the link "New article", like:
<%= link_to 'New article', new_magazine_article_path(#article)
and an article helper to pass correct parameters to the form_for:
module ArticlesHelper
def form_for_params
if action_name == 'edit'
[#article]
elsif action_name == 'new'
[#magazine, #article]
end
end
end
so I can use Article form_for like:
<%= simple_form_for(form_for_params) do |f| %> ...
The ArticlesController methods for new and create are:
respond_to :html, :xml, :js
def new
#magazine = Magazine.find(params[:magazine_id])
#article = Article.new
end
def create
#magazine = Magazine.find(params[:magazine_id])
#article = #magazine.build_article(params[:article])
if #article.save
respond_with #magazine # redirect to Magazine show page
else
flash[:notice] = "Warning! Correct the title field."
render :action => :new
end
end
The problem occurs when there is a validation error with the title attribute, and the action new is rendered. In this moment I get the message: undefined method `model_name' for NilClass:Class in the first line of form_for. I think it is because the #magazine parameter passed in the helper.
How could I solve this problem withou use redirect_to ? (I want to mantain the other attributes that were filled in the form .)
Your form_for_params method is returning nil, because the action_name is set to 'create', not 'new' or 'edit'.
Try this:
elseif action_name == 'new' or action_name == 'create'
[#magazine, #article]