Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

respond_with :json (strange behavior) #4880

Closed
xpepermint opened this Issue Feb 4, 2012 · 11 comments

Comments

Projects
None yet
7 participants

I get the exact same thing as this: http://www.ruby-forum.com/topic/3315760

Owner

spastorino commented Feb 5, 2012

Can you push to github an app that reproduces the issue?. It's working fine for me.

Hey... I'm afraid no code is available but I now know where the problem is.

How to reproduce it:

  1. Create a controller ProductsController with index action, put the respond_with(@products) method inside and define respond_to :json, :html.
  2. In the ApplicationController create index action with no content.
  3. Create file for this action inside views/application/index.html.haml.

Voila... when you try to get your products/index.json content the source of the application/index.html.haml file is rendered instead of JSON products/index.json content. If you remove application index/views everything is ok. Note also that log says Processing byProductsController#index as JSON` but... as explained.

Owner

spastorino commented Feb 5, 2012

Sorry but I still can't reproduce it, we are probably trying different things.
Can you push a simple app to github that shows exactly the code you have?, also if you can avoid haml much better since should not be related to the issue

I am having similar issues, I am using rails as an API backend for my backbone front end stuff,

i've added my resources under a scope like this

  scope "api" do
    scope ":account_id" do
      resources :contacts do
        resources :emails, only: [:create, :show, :update, :destroy]
      end
    end
  end

when i boot my app this causes a problem i have to add resources :emails out side of the scope block for it to work

here is my controller

  def create
     respond_with current_contact.emails.create(params[:email])
  end

i am having issues specifically with the create action

I get this error

NoMethodError (undefined method `email_url' for #<EmailsController:0x007fc4cc570a60>):
  app/controllers/emails_controller.rb:10:in `create'

not sure if this is the same issue

Owner

spastorino commented Feb 25, 2012

@artellectual if you can share a minimal app that reproduces the issue, much better :)

kamui commented Mar 5, 2012

I have this issue. I'm not sure how to pass a named parameter scope into respond_with.

The easiest way to reproduce it is to wrap any resource in a named parameter scope in your routes.rb:

 scope ':project_name' do
  resources :signups
end

Then have a controller that uses respond_to and respond_with:

class SignupsController < ApplicationController
  respond_to :json

  def create
    @signup = @project.signups.new(params[:signup])
    @signup.save
    respond_with @signup
  end
end

In this example, a before filter populates @project, which isn't showed here, but you get the idea.

@spastorino spastorino was assigned Mar 6, 2012

@xpepermint @artellectual @kamui can someone please give it a try with latest Rails 3.2.3 to see if the issue still exists? Thanks.

Contributor

carlosramireziii commented Apr 16, 2012

@carlosantoniodasilva I created an example application using Rails 3.2.3 which (I believe) demonstrates the issue that @artellectual was having.

Please let me know if I can do anything more to help with this issue.

@carlosramireziii, @artellectual The problem is that respond_with is trying to send a redirect. In @carlosramireziii's example the route is :only => :create, so post_url is not found.

With @artellectual he has a nested resources so it again can't infer the URL with 'email_url`.

Use the :location option on respond_with to specify the appropriate URL to redirect to, since it won't be able to infer the proper location from the object itself.

In the case of API where you don't need it to redirect necessarily, pass :location => nil.

Example:

respond_with @user, location: [ 'emails', current_user ] # would look for something like emails_user_url

Check your rake routes to see what type of URL helpers are available to you.

The original issue poster posted this 3 months ago. This can probably be closed. I don't think this is a bug.

Yeah, @erichmenge is right on this, you need to give a valid resource location in the case of your example app, @carlosramireziii @artellectual.

I have tested the described issue in Rails 3.2.3 with the following controller / view:

# app/controllers/post_controller.rb
class PostsController < ApplicationController
  respond_to :json

  def index
    @articles = Article.all
    respond_with @articles
  end
end

# app/views/posts/index.html.erb
<h1>Posts#index</h1>
<p>Find me in app/views/posts/index.html.erb</p>

and it works fine, I always get back a json response (same requesting /posts or /posts.json):

$ curl -I localhost:3001/posts
HTTP/1.1 200 OK 
Content-Type: application/json; charset=utf-8

$ curl -I localhost:3001/posts.json
HTTP/1.1 200 OK 
Content-Type: application/json; charset=utf-8

If I add :html to the respond_to mix, it works fine as well:

class PostsController < ApplicationController
  respond_to :html, :json

  def index
    @articles = Article.all
    respond_with @articles
  end
end
$ curl -I localhost:3001/posts
HTTP/1.1 200 OK 
Content-Type: text/html; charset=utf-8

$ curl -I localhost:3001/posts.json
HTTP/1.1 200 OK 
Content-Type: application/json; charset=utf-8

So I'm closing this. If you still think it is a bug, please let us know and I'll gladly reopen.

@erichmenge thanks!

Contributor

carlosramireziii commented May 17, 2012

@carlosantoniodasilva @erichmenge thanks for clearing that up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment