rails respond_to in controller rescue block - ruby-on-rails

respond_to not working inside controller rescue block:
def create
respond_to do | format |
format.json { render json: SomeManager.new(some_params).json }
format.html do
SomeManager.new(some_params)
render :new, notice: 'it worked'
end
end
rescue => e
respond_to do | format |
format.json { render json: {error: 'did not work because reasons'}.to_json, status: :forbidden }
format.html { render :new, alert: 'did not work because reasons' }
end
end
^ Controller for API which can respond to API json uploads, or manual uploads using UI.

For better readability, use begin .. rescue like this instead:
def create
respond_to do | format |
begin
format.json { render json: SomeManager.new(some_params).json }
format.html do
SomeManager.new(some_params)
render :new, notice: 'it worked'
end
rescue => e
format.json { render json: {error: 'did not work because reasons'}.to_json, status: :forbidden }
format.html { render :new, alert: 'did not work because reasons' }
end
end
end
I think it shall work.

The rescue block isn't to blame. This would happen if your routes.rb is specified with an option like so:
resources :widgets, defaults: {format: :the_spanish_inquisition}

Related

Rails render index (JSON) after update returning empty array

In my small Rails app, I want to update an object through json format, and on success receive the index.json.jbuilder content (using the render :index on JSON format in update). But for some reason, I just get an empty array... I tried many options without success, so any help would be welcome! Thx in advance
My routes
resources :coaches, only: [:show] do
resource :calendar, only: :show, defaults: { format: :json }
resources :events, only: [:index, :update], defaults: { format: :json }
end
My controller
respond_to :json, only: :index
def index
#events = Event.all
end
def update
respond_to do |format|
if #event.update(event_params)
if params[:commit] == I18n.t('next')
format.html { redirect_to booking_event_confirm_path(#event) }
elsif params[:commit] == I18n.t('confirm')
format.html { redirect_to root_path, notice: "Event was successfully confirmed." }
else
format.html { redirect_to booking_event_path(#event), notice: 'Event was successfully updated.' }
format.json { render :index, status: :ok }
end
else
if params[:commit] == I18n.t('next')
format.html { render :training }
elsif params[:commit] == I18n.t('confirm')
format.html {
# TODO: why is the url not persistent? ('/confirm' being removed even if page display seems ok)
render :confirm
}
else
format.html { render :training }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
end
You need to define #events in update action to render it as json
def update
respond_to do |format|
if #event.update(event_params)
if ...
....
else
#events = Event.all
format.json { render :index, status: :ok }
end
else
....
end
end
end

How to add different edit/update method into rails controller?

The controller:
def edit
end
def update
respond_to do |format|
if #item.update(item_params)
format.html { redirect_to #item, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: #item }
else
format.html { render :edit }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
I want to add another methods like this. For updating only 1 column of item and i want to dont show other columns to user.
I have all neccesary views.
def another_edit
end
def another_update
respond_to do |format|
if #item.update(params[:item].permit(:amount))
format.html { redirect_to #item, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: #item }
else
format.html { render :edit }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
I thought my routes are wrong. Then tried
resources :items do
collection do
get 'add', on: :edit
end
end
and
get 'items/another_edit'
Is it possible or not? Please help me.
You need to add those endpoints in routes as "member" (not collection):
resources :items do
get :another_edit, on: :member
put :another_update, on: :member
end
Then routes will be built with "id" parameter inside.

Rails 4.2 NameError in CandiesController#create

Trying to build a route that can display pages with varying information about various types of candy.
the route recognizes URL paths but want it to only show valid candy types e.g kit_kat, gummy_bear, twizzler Any other type of candy specified should generate a 404 status code
Generated a scaffold to allow anyone to add candy types but when i try to pass the valid candy types ( kit_kat etc) I get error
Rails 4.2 NameError in CandiesController#create
undefined local variable or method ` params' for #
**candy_controller.rb**
class CandiesController < ApplicationController
before_action :set_candy, only: [:show, :edit, :update, :destroy]
# GET /candies
# GET /candies.json
def index
#candies = Candy.all
end
# GET /candies/1
# GET /candies/1.json
def show
end
# GET /candies/new
def new
#candy = Candy.new
end
# GET /candies/1/edit
def edit
end
# POST /candies
# POST /candies.json
def create
if (([:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler]).any? { |word| params[:title].includes?(word) })
#candy = Candy.new(candy_params)
respond_to do |format|
if #candy.save
format.html { redirect_to #candy, notice: 'Candy was successfully created.' }
format.json { render :show, status: :created, location: #candy }
else
format.html { render :new }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
end
# PATCH/PUT /candies/1
# PATCH/PUT /candies/1.json
def update
respond_to do |format|
if #candy.update(candy_params)
format.html { redirect_to #candy, notice: 'Candy was successfully updated.' }
format.json { render :show, status: :ok, location: #candy }
else
format.html { render :edit }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /candies/1
# DELETE /candies/1.json
def destroy
#candy.destroy
respond_to do |format|
format.html { redirect_to candies_url, notice: 'Candy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_candy
#candy = Candy.friendly.find(params[:id])
end
# Use callbacks to share common setup or constraints between actions.
# Never trust parameters from the scary internet, only allow the white list through.
def candy_params
params.require(:candy).permit(:title, :discription)
end
end
candy.rb
class Candy < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
end
updated candy_controller.rb
def create
if candy[:title] && !candy[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler].include?(candy[:title].to_sym)
#candy = Candy.new(candy_params)
respond_to do |format|
if #candy.save
format.html { redirect_to #candy, notice: 'Candy was successfully created.' }
format.json { render :show, status: :created, location: #candy }
else
format.html { render :new }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
end
updated code
def create
if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler].include?(candy_params[:title].to_sym)
#candy = Candy.new(candy_params)
respond_to do |format|
if #candy.save
format.html { redirect_to #candy, notice: 'Candy was successfully created.' }
format.json { render :show, status: :created, location: #candy }
else
format.html { render :new }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
end
A couple of things,
First, params doesn't have :title, :title is in params[:candy][:title], or you just use candy_params[:title]
Second, the if statement could be shorter
if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler].include?(candy_params[:title].to_sym)
(Go on and create the candy)
else
(Redirect with error messages | Wrong Candy Type)
end
It's always good to check the existence of the params and make sure it's not empty first, then check if it's included in the acceptable list. Notice that your original code was to compare symbol with string, so cast them to the same type and check.
UPDATE
Added else statement for redirect when :title isn't present, empty string, or wrong type

Rails Template Missing

I recently updated a controller from a plain
render :json => #something
to
respond_to do |format|
format.html {
redirect_to #something
}
format.json {
puts "found json format"
format.json { render json: #something, status: :created }
}
end
But, now I'm getting ActionView::MissingTemplate errors. My question is, if I use the respond_to do |format| block, am I required to use a template? What if I just want to return plain json?
Btw, the puts statement in the json respond_to block is being called successfully.
I think the problem is puts being called inside of your format.json block and the nesting of format.json inside of it's own block. Try removing both (as below).
respond_to do |format|
format.html {
redirect_to #something
}
format.json {
puts "found json format"
format.json { render json: #something, status: :created }
}
end
Try:
respond_to do |format|
format.html { redirect_to #something }
format.json { render json: #something, status: :created }
end
You are telling that you accept HTML and JSON formats. Since your request comes from HTML you'll have the HTML template rendered.
If you want to return only JSON remove that line from your code. Also check the docs on respond_to.
Probably you already know it, but there's a guide on Layouts and Rendering.
Remove the format.json part from within the json rendering block (you have an unnecessary block within your block):
respond_to do |format|
format.html {
redirect_to #something
}
format.json {
render json: #something, status: :created
}
end
I think the reason your code is prompting an error it's because you have
format.json {
format.json { render json: #something , status: :created }
}
You should have:
format.json {
render json: #something , status: :created
}

how to debug a POST 500 internal server error

I'm trying to set up jquery file upload on a Rails 3.2 app, following this guide.
Everything is almost working perfectly but, when I click "start upload" I see an error in Chrome's console:
POST http://testapp.dev/photos 500 (Internal Server Error)
In the log I'm getting:
ActionView::MissingTemplate (Missing template photos/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}.
I've been through the controller with a fine tooth comb, but I can't work out what is causing this error or why its looking for a create partial. What is a systematic way to debug this?
Thanks
EDIT
My controller actions look like this:
class PhotosController < ApplicationController
def index
#photos = Photo.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #photos }
format.json { render :json => #photos.collect { |a| a.to_jq_upload }.to_json }
format.js { render :json => #photos.collect { |a| a.to_jq_upload }.to_json }
end
end
def show
#photo = Photo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #photo }
format.json { render :json => #photo }
format.js
end
end
def new
#photo = Photo.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #photo }
format.json { render :json => #photo }
format.js
end
end
def edit
#photo = Photo.find(params[:id])
end
def create
#photo = Photo.new(params[:photo])
respond_to do |format|
if #photo.save
format.html {
render :json => [#photo.to_jq_upload].to_json,
:content_type => 'text/html',
:layout => false
}
format.json { render :json => [ #photo.to_jq_upload].to_json }
format.js
else
format.html { render :action => "new" }
format.xml { render :xml => #photo.errors, :status => :unprocessable_entity }
format.json { render :json => [ {:error => "An error was encountered while processing your photos. Please try again."}], status: 304 }
format.js
end
end
end
def update
#photo = Photo.find(params[:id])
respond_to do |format|
if #photo.update_attributes(params[:photo])
format.html { redirect_to(#photo, :notice => 'Asset was successfully updated.') }
format.xml { head :ok }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #photo.errors, :status => :unprocessable_entity }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /assets/1
# DELETE /assets/1.xml
def destroy
#photo = Photo.find(params[:id])
#photo.destroy
respond_to do |format|
format.html { redirect_to(photos_url) }
format.xml { head :ok }
format.json { render :json => true }
format.js
end
end
end
It looks like the respond_to block is hitting format.js which will attempt to render a template (a default if no options are provided).
Because the jQuery-file-upload plugin you're using requires a particular JSON response, having a respond_to block seems unnecessary and you can get away with the following
def create
#photo = Photo.new(params[:photo])
if #photo.save
render :json => [ #photo.to_jq_upload].to_json
else
render :json => [{ :error => "An error was encountered while processing your photos. Please try again." }], :status => 304
end
end
I would also highly recommend the debugger gem which will allow you to set breakpoints within the application so you can better tell what is happening.
To add the debugger gem, you'll need to first add the gem to your Gemfile and run a bundle install from the command line. Next, you simply add the word debugger on the line of code you want to set the breakpoint at. In your case, you could do
def create
#photo = Photo.new(params[:photo])
debugger
if #photo.save
# ... rest of code
Finally, you'll need to restart the server with the --debugger option. When the jQuery-file-upload plugin posts the request to the server, it'll hit the breakpoint and you can better analyze the passed params as well as step through the code to get an idea of what is happening. Cheers.

Resources