I have the following in my controller:
respond_to :html, :except => :some_action
respond_to :json, :xml
If you hit the :some_action route in a browser (tested with Chrome), you get a 406 Not Acceptable response back. Is there a way to "catch" this in Rails and do something else (like a redirect)?
Additionally, I'm trying to avoid using the block form of respond_to. I'm just curious if there is some way to handle this case.
Check this out: http://ryandaigle.com/articles/2009/8/6/what-s-new-in-edge-rails-cleaner-restful-controllers-w-respond_with
There's a bit about action overriding:
class UsersController < ApplicationController::Base
respond_to :html, :xml, :json
# Override html format since we want to redirect to a different page,
# not just serve back the new resource
def create
#user = User.create(params[:user])
respond_with(#user) do |format|
format.html { redirect_to users_path }
end
end
end
Related
What is the best approach here? I'm trying to clean up some code and I'm wondering if the controller is the best place for this variety of logic:
if user_signed_in?
if current_user.try(:admin?)
#docs = Doc.chronologic.page(params[:page]).per(5)
#orders = Order.chronologic.page(params[:page]).per(5)
else
#docs = Doc.chronologic.where(:user_id => current_user.ftp, :retired => "active").page(params[:page]).per(5)
#orders = Order.chronologic.where(:user => current_user.ftp).page(params[:page]).per(5)
end
respond_to do |format|
format.html
format.json { render json: #docs }
end
else
redirect_to new_user_session_path
end
If there's a better location for it, where would it be?
Thanks!
Edit: it's far worse for methods like pdf which has line after line of instructions for Prawn, but I can't seem to get send_data to work from the model.
This is basically what mu said, but here's my take.
In your app controller:
def require_logged_in
redirect_to new_user_session_path unless user_signed_in?
end
In your controller
before_filter :require_logged_in
def some_action
#docs = Doc.chronologic.for_user(current_user).page(params[:page]).per(5)
#orders = Order.chronologic.for_user(current_user).page(params[:page]).per(5)
respond_to do |format|
format.html
format.json { render json: #docs }
end
end
In your Doc model
scope :for_user, lambda do |user|
where(:user_id => user.ftp, :retired => "active") unless user.admin?
end
And something similar in your Order model.
Per your edit, definitely don't do send_data from your model.
I'm using respond_to and respond_with pair in Rails 3 to respond to a DELETE #destroy request. The destroy action is defined in UsersController. It destroy the User specified by params[:id] and respond with a JS template. However, the request keeps failing in RSpec test and I'm not really sure how to fix it.
Here's a snippet for my UsersController:
class UsersController < ApplicationController
respond_to :html, only: [:index, :show, :new, :edit]
respond_to :js, only: [:create, :update, :delete]
...
def destroy
#user = User.find(params[:id])
#user.destroy
respond_with #user
end
end
And here is the test that keeps failing:
require 'spec_helper'
describe UsersController do
...
describe "DELETE #destroy" do
before { #user = create(:user); delete :destroy, id: #user.id, format: :js }
it "return HTTP success" do
response.should be_success
end
it "respond with JS content" do
response.content_type.should == 'text/javascript'
end
end
end
The test "respond with JS content" does succeed, however the test "return HTTP successs" fails. When I use a debugger to check response.code, it is 406. I'm expecting 2xx since the deletion succeed. Is this a normal Rails behavior or do I have something wrong with my code?
Seems you messed up REST delete and controller's destroy. Rails responder knows nothing about :delete action, change
respond_to :js, only: [:create, :update, :delete]
to
respond_to :js, only: [:create, :update, :destroy]
or pass a block to the responder, instead of
respond_with #user
use
respond_with #user do |format|
format.js
end
As it's not currently possible for me to use a json templating engine (jbuilder or rabl) as per Rails3 ActionView Template Handlers doesn't work on Production Server I'm wondering how to best change this controller action to include a custom node with as_json (or something else)
class Mobile::AndroidUsersController < SecureMobileUserController
skip_before_filter :authorize, :only => :create
respond_to :json
# POST /mobile_users
# POST /mobile_users.xml
def create
#mobile_user = AndroidUser.find_by_auth(params[:mobile_user][:auth])
unless #mobile_user
#mobile_user = AndroidUser.new(params[:mobile_user])
else
#mobile_user.attributes = params[:mobile_user]
end
respond_to do |format|
if #mobile_user.save
format.json #Add a custom token node here
else
:unprocessable_entity }
format.json { render json: #mobile_user.errors, status: :unprocessable_entity }
:unprocessable_entity }
end
end
end
end
I just need to add a custom node called token that has a value that I get from calling a method on the MobileUser class
:token => MobileUser.next_token
You can change the call to as_json like this:
format.json {render :json => #mobile_user.as_json(:methods => [:next_token])}
So here's my controller:
class ScriptController < ApplicationController
respond_to :js
def show
puts 'here'
#client_id = params[:id]
respond_with #client_id
end
end
I have a file called in app/views/script/show.js.coffee but it doesn't load when I go to the show page. I'm using the coffeebeans gem: Loading .coffee files via a view in Rails
Any help?
EDIT: Added Error
Template is missing
Missing template good_comments/script/show, good_comments/application/show with {:handlers=>[:erb, :builder], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in: * "/Users/shamoon/Sites/good_comments/spec/dummy/app/views" * "/Users/shamoon/Sites/good_comments/app/views"
Don't use CoffeeBeans, coffee-rails handles rendering of coffeescript out-of-box.
I render coffeescripts like this:
respond_to do |format|
format.html { redirect_to '/' }
format.js { render 'delete', :layout => false }
end
I have the following routings
PosTracker::Application.routes.draw do
get "home/index"
resources :pos
resources :apis
match 'update_data' => 'home#update', :as => :update, :via => :get
root :to => "home#index"
end
Now, when using the link_to helper method:
link_to "text", pos_path(starbase)
I get the following route /pos.13 instead of /pos/13. Obviously, this won't produce valid output. How can I fix this?
Edit: Relevant controller:
class PosController < ApplicationController
# GET /pos
# GET /pos.xml
def index
#do stuff
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #pos }
end
end
# GET /pos/1
# GET /pos/1.xml
def show
#pos = Pos.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #pos }
end
end
end
It seems to me like Rails is recognizing pos_path as your #index action url helper. Generally it will take the symbol you pass to resources and singularize it for a #show action.
The url helper you want to use would be
link_to "text", po_path(starbase)
You can generally find the name of the helper methods by running
rake routes
Or to get the helper for a specific controller
rake routes CONTROLLER=pos