respond-with

Including multiple associations using respond_with

こ雲淡風輕ζ 提交于 2019-12-06 09:58:52
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, :include => :posts end Which gives me the following json response [{ "created_at":"2013-06-17T13:10:59Z", "id"

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

老子叫甜甜 提交于 2019-12-04 20:56:33
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. 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 flash #if @user.save # flash[:notice] = "Successfully created user." #end respond_with(@user) end # Also,

Understanding Rails 3's respond_with

∥☆過路亽.° 提交于 2019-12-02 18:29:42
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 <""> . If I change the spec to expect "" it still fails. When it renders /carriers that page shows the

How to customize to_json response in Rails 3

帅比萌擦擦* 提交于 2019-11-30 08:37:57
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 do this: def show @post = Post.find params[:id] respond_with(@post, :include => :images) end but I dont