Questions tagged [respond-with]
51 questions
25
votes
2 answers
rails 3 response format and versioning using vendor MIME type in the Accept header
Preamble:
I investigated how to version an API and found several ways to do it. I decided to try peter williams' suggestion and created new vendor mime types to specify version and format. I could find no definitive write-up for doing this…

jay
- 602
- 5
- 14
16
votes
3 answers
How to customize to_json response in Rails 3
I am using respond_with and everything is hooked up right to get data correctly. I want to customize the returned json, xml and foobar formats in a DRY way, but I cannot figure out how to do so using the limited :only and :include. These are great…

coneybeare
- 33,113
- 21
- 131
- 183
15
votes
2 answers
Rails routing error with respond_with when creating nested resource
I'm having the following problem with a simple Rails 3 app I'm creating. It is driving me batty.
My model:
class Vehicle < ActiveRecord::Base
has_many :vehicle_pictures, :dependent => :destroy
def
class VehiclePicture < ActiveRecord::Base
…

orj
- 13,234
- 14
- 63
- 73
12
votes
1 answer
Understanding Rails 3's respond_with
Utilizing ActionController's new respond_with method...how does it determine what to render when action (save) is successful and when it's not?
I ask because I'm trying to get a scaffold generated spec (included below) to pass, if only so that I…

Meltemi
- 37,979
- 50
- 195
- 293
11
votes
4 answers
Including "type" attribute in json respond_with Rails 3.1
It seems that when returning an object containing a "type" attribute as JSON from a Rails 3.1 application, the "type" attribute is not included. Assume I have the following:
A model with corresponding STI table Animal.
Models Cat, Dog and Fish that…

cyrusd
- 133
- 1
- 5
11
votes
1 answer
Rails 3: Proper way to Delete resource using respond_with
I'm trying to DRY up a controller by incorporating respond_with. When I do, following some instructions in a Railscast, I get things mostly working. The problem lies in the redirect after deleting a resource...which should be redirecting to…

Meltemi
- 37,979
- 50
- 195
- 293
8
votes
2 answers
respond_with is asking for location on error
I have a pretty standard authenticate method
private
def authenticate_user
@current_user = User.find_by_authentication_token(params[:token])
unless @current_user
error = { :error => "Invalid token." }
respond_with(error,…

Pykih
- 2,769
- 3
- 29
- 38
7
votes
5 answers
How to DRY up Rails 3 controllers by overriding methods like respond_with?
I'm trying to create a JSONP API for my Rails 3 application. Right now in my controllers, I have a lot of actions which follow this pattern:
# This is from my users_controller.rb, as an example
def index
@users = User.all
respond_with(@users,…

evanrmurphy
- 1,826
- 1
- 18
- 19
6
votes
1 answer
respond_with redirecting when format=json
I'm encountering a strange behavior in my controllers. They seem to occasionally want to redirect instead of render a json response.
respond_to :json, :html, :js
def create
@favorite =…

Dan Hixon
- 825
- 11
- 17
6
votes
1 answer
Stop Rails from trying to provide a template / ActionView::MissingTemplate
I have a simple angular rails app that I am trying to get wired up.
Here is my rails controller:
class ItemsController < ApplicationController
respond_to :json, :html
def index
@items =…

user3828000
- 101
- 1
- 10
5
votes
2 answers
How to customize the JSON that respond_with renders in case of validation errors?
In a controller, I want to replace if..render..else..render with respond_with:
# Current implementation (unwanted)
def create
@product = Product.create(product_params)
if @product.errors.empty?
render json: @product
else
render json: {…

João Souza
- 4,032
- 3
- 25
- 38
5
votes
1 answer
Rails respond_with & Rspec controllers: Testing unsuccessful update
I'm trying to switch from using respond_to to respond_with in Rails controllers. Everything's going smoothly, except for testing invalid saves in controller specs. Here's an example:
describe MyController do
...
describe "PUT update" do
context…

PlankTon
- 12,443
- 16
- 84
- 153
4
votes
1 answer
How can I simplify this Rails 3 controller method
I currently have this method in a controller:
def show
property = Property.find(params[:id])
respond_to do |format|
format.xml { render :xml => property.to_xml(:except => [:address1, :address2, :analysis_date, :analysis_date_2, ...]) }
…

tollbooth
- 424
- 4
- 12
4
votes
2 answers
How to respond_with multiple objects in Rails 3.1
I have a route for example
POST /interaction.json
where the client posts a new interaction. Normally my controller would look like
class InteractionController < ApplicationController
def create
respond_with @log
end
end
and I…

bradgonesurfing
- 30,949
- 17
- 114
- 217
4
votes
1 answer
JSON, respond_with, and nested resources routing error?
Hey Everybody, Thanks in advance for checking out my question.
I've got nested routes like so:
resources :users do
resources :avatars
end
And upon creating a user, I also create an avatar like so:
def create
@user = User.create(params[:user])
…

Jared Goodner
- 161
- 2
- 11