Render alternate view in ruby on rails - ruby-on-rails-3

I have the following code in my controller
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
render 'save'
end
end
I am trying to get the method to render another view file other than create.js.erb however the controller always renders the default rather than the save.js.erb.
Any ideas on what could be wrong?

def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
respond_to do |format|
format.js { render :template => "/path/to/save" }
end
end
end
or
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
respond_to do |format|
format.js { render :file => "/path/to/save.js.erb" }
end
end
end
try this
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
end
render :file => "/path/to/save.js.erb"
end

Related

Rails error: ArgumentError in ArticlesController#create wrong number of arguments (2 for 1)

i have tutorial on Rails from here:
Rails5
and wanted to add a field "attachment" to the form.
articles_controller.rb is as follows:
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "gerrit", password: "Ifb6K54WVs7U", except: [:index, :show]
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :attachment)
end
end
For this i added paperclip and turbolink5.
I rerun all configuring steps in the tutorial but I always get the error in the title:
Extracted source (around line #24):
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
I do not use single params here, but article_params!?
Any help appreciated!
As said in the last comment:
I updated paperclip to v5.1.0 and the error was gone.

Rails: Nested resource and specifying the "nester"

A company has many properties. A property has one company.
In my routes file I got:
resources :companies do
resources :property_managers
end
In the property_manager_controller, my create action looks like this (default scaffold implementation slightly modified to accommodate the company):
def create
#property_manager = PropertyManager.new(params[:property_manager])
#property_manager.company_id = params[:company_id]
respond_to do |format|
if #property_manager.save
format.html { redirect_to company_property_managers_path, notice: 'Property manager was successfully created.' }
format.json { render json: #property_manager, status: :created, location: #property_manager }
else
format.html { render action: "new" }
format.json { render json: #property_manager.errors, status: :unprocessable_entity }
end
end
end
Is there a way in which I do not have to explicitly set the company_id, since it is known within the context of the URL/route?
I guess you could do something like the following, not sure if it's better or not:
class PropertyManagersController < ApplicationController
before_filter :find_company
def new
#property_manager = #company.property_managers.build
end
def create
#property_manager = #company.property_managers.build(params[:property_manager])
respond_to do |format|
...
end
end
private
def find_company
#company ||= Company.find(params[:company_id])
end
end

Avoid index url after failing create

When you create a User in rails through the create action, the url is changed to
http://myapplication.com/users with POST
before being redirected elsewhere. If validation fails, it appears that the above URL is retained. If you then refresh, you end up on the index page (as it's now a GET).
I would expect if validation was failed the url would remain as
http://myapplication.com/users/new
As i don't have an index page, this is causing me problems. Is there a way to resolve this please?
This depends on the logic in the respond_to block in your controller.
This is a typical example of the create action in users_controller.rb:
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
So if the save fails, the new action is rendered again.
In your UsersController, do like this:
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url # save success will return to root page
else
render 'new'
end
end

Photo Gallery Association of Models

I'm trying to setup a photo gallery for each user. How would I set up the associations?
class User < ActiveRecord::Base
has_many :photos
has_many :galleries, through :photos
end
class Gallery < ActiveRecord::Base
has_many :photos
belongs_to :user
end
class Photo < ActiveRecord::Base
belongs_to :gallery
belongs_to :user
end
i have the gallery and photo model currently working. You are able to create a gallery and add photos to it. I'm confused is to how to add it to the current user?
class GalleriesController < ApplicationController
def index
#galleries = Gallery.all
end
def show
#gallery = Gallery.find(params[:id])
end
def new
#gallery = Gallery.new
end
def create
#gallery = Gallery.new(params[:gallery])
if #gallery.save
flash[:notice] = "Successfully created gallery."
redirect_to #gallery
else
render :action => 'new'
end
end
def edit
#gallery = Gallery.find(params[:id])
end
def update
#gallery = Gallery.find(params[:id])
if #gallery.update_attributes(params[:gallery])
flash[:notice] = "Successfully updated gallery."
redirect_to gallery_url
else
render :action => 'edit'
end
end
def destroy
#gallery = Gallery.find(params[:id])
#gallery.destroy
flash[:notice] = "Successfully destroyed gallery."
redirect_to galleries_url
end
end
PhotosController
class PhotosController < ApplicationController
def new
#photo = Photo.new(:gallery_id => params[:gallery_id])
end
def create
#photo = Photo.new(params[:photo])
if #photo.save
flash[:notice] = "Successfully created Photo."
redirect_to #photo.gallery
else
render :action => 'new'
end
end
def edit
#photo = Photo.find(params[:id])
end
def update
#photo = Photo.find(params[:id])
if #photo.update_attributes(params[:photo])
flash[:notice] = "Successfully updated Photo."
redirect_to #Photo.gallery
else
render :action => 'edit'
end
end
def destroy
#photo = Photo.find(params[:id])
#photo.destroy
flash[:notice] = "Successfully destroyed Photo."
redirect_to #photo.gallery
end
end
Once you grab the current user, just concatenate the gallery to the user's galleries.
def update
#gallery = Gallery.find(params[:id])
#user = current_user
if #gallery.update_attributes(params[:gallery])
#user.galleries << #gallery
flash[:notice] = "Successfully updated gallery."
redirect_to gallery_url
else
render :action => 'edit'
end
end

undefined method for nil:NilClass - Controller

Could some explain to me why I may well be getting the following error every time I try to create a booking.
NoMethodError in BookingsController#create
undefined method `diary_slot_day_time_path' for nil:NilClass
I have the following code in my controller:
class BookingsController < PortalController
# GET /bookings
# GET /bookings.json
def index
session[:booking_context] = 'index'
if current_user.is_booking_manager?
# #bookings = Booking.all
# #bookings = Booking.filter(params[:search], [:reference_number, :company_id])
#bookings = Booking.includes(:company).filter(params[:search], [:reference_number, "companies.name"])
#main_heading = 'Bookings'
else
#bookings = current_user.company.bookings
#main_heading = "#{current_user.company.name} Bookings"
end
respond_to do |format|
format.html # index.html.erb
end
end
# GET /bookings/1
def show
#booking = Booking.find(params[:id])
goto_booking(#booking)
end
# GET /bookings/1/edit
def edit
#booking = Booking.find(params[:id])
#main_heading = 'Edit Booking'
end
# Go to booking
def goto_booking(booking)
site = #booking.site
slot_day = #booking.slot_day
slot_time = #booking.slot_time
# don't need to check slot_time because it is optional
if session[:booking_context] and session[:booking_context] == 'diary' and site and slot_day
# Go the diary page
respond_to do |format|
format.html { redirect_to diary_slot_day_time_path(site, slot_day, slot_time) }
end
else
# Go to booking edit page
respond_to do |format|
format.html { redirect_to edit_booking_path(#booking) }
end
end
end
# POST /bookings
def create
#booking = Booking.new(params[:booking])
unless #booking and #booking.slot_time and #booking.slot_time.number_of_free_slots > 0
flash[:notice] = 'Slot no longer available'
redirect_to :back
return
end
if #booking.save
flash[:notice] = 'Booking was successfully created'
goto_booking(#booking)
else
#main_heading = 'Review New Booking'
respond_to do |format|
format.html { render action: "new" }
end
end
end
# PUT /bookings/1
def update
#booking = Booking.find(params[:id])
if #booking.update_attributes(params[:booking])
flash[:notice] = 'Booking was successfully updated'
goto_booking(#booking)
else
#main_heading = 'Review Booking'
respond_to do |format|
format.html { render action: "edit" }
end
end
end
def confirmation
begin
#booking = Booking.find(params[:booking][:id])
#booking.update_attributes(params[:booking])
if params[:make_unexpected]
#booking.provisional_appointment = nil
#booking.save!
end
#booking.update_slot!
success = true
rescue => e
flash[:error] = e.message
success = false
end
if success
flash[:notice] = 'Booking confirmed'
respond_to do |format|
format.html { redirect_to booking_path(#booking) }
end
else
redirect_to :back
end
end
# DELETE /bookings/1
def destroy
#booking = Booking.find(params[:id])
if #booking
if session[:booking_context] and session[:booking_context] == 'diary'
site = #booking.site
slot_day = #booking.slot_day
slot_time = #booking.slot_time
end
if #booking.destroy
flash[:notice] = 'booking removed'
else
flash[:error] = 'could not remove booking'
end
end
# don't need to check slot_time; that's optional
if site and slot_day
# redirect to diary page
respond_to do |format|
format.html { redirect_to diary_slot_day_time_path(site, slot_day, slot_time) }
end
else
# redirect to booking index
respond_to do |format|
format.html { redirect_to bookings_url }
end
end
end
def diary_slot_day_time_path(*args)
#template.diary_slot_day_time_path(*args)
end
end
I think that potentially it is something to do with a routing error I may well be wrong. This is my full trace error: Pastie. I have looked at the trace which indicates to me that there is something wrong with my go_to_booking, create methods.
Why do you have this method defined at the bottom of your controller?
def diary_slot_day_time_path(*args)
#template.diary_slot_day_time_path(*args)
end
This is attempting to reference an undefined instance variable called #template, which is why you're getting that error.
You should be defining this method in your routes, by using nested routes like this:
match '/diary/:day/:time', :to => "some_controller#some_action", :as => "diary_slot_day_time"