respond-with

How to customize to_json response in Rails 3

倖福魔咒の 提交于 2020-01-10 12:54:31
问题 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 when the data is simple, but with complex finds, they fall short of what I want. Lets say I have a post which has_many images def show @post = Post.find params[:id] respond_with(@post) end I want to include the images with the response so I could

rails 3 response format and versioning using vendor MIME type in the Accept header

假如想象 提交于 2019-12-31 11:43:54
问题 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 following "the rails way" so I pieced together info from several places. I was able to get it working, but there is some goofiness in the way the renderers handle Widget array vs Widget instance in respond_with . Basic steps & problem: I registered mime

rails 3 response format and versioning using vendor MIME type in the Accept header

怎甘沉沦 提交于 2019-12-31 11:43:15
问题 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 following "the rails way" so I pieced together info from several places. I was able to get it working, but there is some goofiness in the way the renderers handle Widget array vs Widget instance in respond_with . Basic steps & problem: I registered mime

Understanding Rails 3's respond_with

半世苍凉 提交于 2019-12-31 09:03:12
问题 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 can understand it. The app is working fine but, oddly, it appears to be rendering /carriers (at least that's what the browser's URL says) when a validation fails. Yet, the spec is expecting "new" (and so am I, for that matter) but instead is receiving

override to_xml to limit fields returned

旧时模样 提交于 2019-12-24 09:44:49
问题 using ruby 1.9.2 and rails 3, i would like to limit the fields returned when a record is accessed as json or xml (the only two formats allowed). this very useful post introduced me to respond_with and i found somewhere online that a nice way to blanket allow/deny some fields is to override as_json or to_xml for the class and set :only or :except to limit fields. example: class Widget < ActiveRecord::Base def as_json(options={}) super(:except => [:created_at, :updated_at]) end def to_xml

Including multiple associations using respond_with

放肆的年华 提交于 2019-12-22 11:48:28
问题 I have a rails 3 app running on ruby v. 1.9.3 with a user , post and role model. A user has many posts as well as roles. My routes.rb file look like this: namespace :api, defaults: {format: 'json'} do resources :users do resources :posts resources :roles end end I want to include the associated posts and roles in the json response when listing all the users. I have found a way to include only one of them, like this: #users_controller.rb def index @users = User.all respond_with @users,

Including multiple associations using respond_with

空扰寡人 提交于 2019-12-22 11:48:27
问题 I have a rails 3 app running on ruby v. 1.9.3 with a user , post and role model. A user has many posts as well as roles. My routes.rb file look like this: namespace :api, defaults: {format: 'json'} do resources :users do resources :posts resources :roles end end I want to include the associated posts and roles in the json response when listing all the users. I have found a way to include only one of them, like this: #users_controller.rb def index @users = User.all respond_with @users,

Including multiple associations using respond_with

折月煮酒 提交于 2019-12-22 11:48:05
问题 I have a rails 3 app running on ruby v. 1.9.3 with a user , post and role model. A user has many posts as well as roles. My routes.rb file look like this: namespace :api, defaults: {format: 'json'} do resources :users do resources :posts resources :roles end end I want to include the associated posts and roles in the json response when listing all the users. I have found a way to include only one of them, like this: #users_controller.rb def index @users = User.all respond_with @users,

How to convert this respond_to options to use Rails 3 version?

萝らか妹 提交于 2019-12-22 00:07:46
问题 respond_to do |format| if @user.save format.js { render :nothing => true, :status => :ok, :location => @user } else format.js { render :json => @user.errors, :status => :unprocessable_entity } end end All options I've tried (like putting respond_to :js at the top of controller, etc) don't quite work the way as in this. 回答1: Rails 3 Format: Use respond_to :json and respond_with (@user) respond_to :json # You can also add , :html, :xml etc. def create @user= User.new(params[:user]) #---For html

respond_with is redirecting to specified location even on validation errors, in rails3

◇◆丶佛笑我妖孽 提交于 2019-12-08 07:51:59
问题 When using location in respond with, it is ignoring validation errors and redirecting to the specified location. Is this expected behavior? I checked in the responder module that it checking if there are any errors on the model. I inspected the model and it contains validation errors in the @solution object. What am I missing here? controller: def create @problem = Problem.find(params[:problem_id]) @solution = @problem.solutions.build params[:solution] @solution.save respond_with(@solution,