Note that there are some explanatory texts on larger screens.

plurals
  1. PORails inherited resources usage
    primarykey
    data
    text
    <p>I'm using <a href="http://github.com/josevalim/inherited_resources" rel="nofollow noreferrer">Inherited Resources</a> for my Rails 2.3 web service app. It's a great library which is part of Rails 3. </p> <p>I'm trying to figure out the best practice for outputting the result. </p> <pre><code>class Api::ItemsController &lt; InheritedResources::Base respond_to :xml, :json def create @error = nil @error = not_authorized if !@user @error = not_enough_data("item") if params[:item].nil? @item = Item.new(params[:item]) @item.user_id = @user.id if !@item.save @error = validation_error(@item.errors) end if !@error.nil? respond_with(@error) else respond_with(@swarm) end end end </code></pre> <p>It works well when the request is successful. However, when there's any error, I get a "Template is missing" error. @error is basically a hash of message and status, e.g. <code>{:message =&gt; "Not authorized", :status =&gt; 401}</code>. It seems <code>respond_with</code> only calls <code>to_xml</code> or <code>to_json</code> with the particular model the controller is associated with. </p> <p>What is an elegant way to handle this? I want to avoid creating a template file for each action and each format (create.xml.erb and create.json.erb in this case)</p> <p>Basically I want: </p> <pre><code>/create.json [POST] =&gt; {"name": "my name", "id":1} # when successful /create.json [POST] =&gt; {"message" =&gt; "Not authorized", "status" =&gt; 401} # when not authorized </code></pre> <p>Thanks in advance. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload