Better way to write this piece of code

I use this type of code for different message and status in my api.

def 404
respond_to do |format|
format.json {render json: { error: ‘404, not found’ }, status: 404}
format.xml {render xml: { error: ‘404, not found’ }, status: 404}
end
end

This works but can I refactoring this code, maybe to one single line?

Roy R. [email protected] writes:

I use this type of code for different message and status in my api.

def 404
respond_to do |format|
format.json {render json: { error: ‘404, not found’ }, status: 404}
format.xml {render xml: { error: ‘404, not found’ }, status: 404}
end
end

This works but can I refactoring this code, maybe to one single line?

While I can applaud the desire, I think in this case you should NOT
attempt to simplify that in any way, as it would obfuscate what’s going
on. This is clean, terse, and extremely clear what is going on.

If this sort of thing is repeated over and over with different status
codes, that should be simplified into a single method, passing in the
code.


Tamara T.
[email protected]
http://www.tamouse.org

While I can applaud the desire, I think in this case you should NOT
attempt to simplify that in any way, as it would obfuscate what’s going
on. This is clean, terse, and extremely clear what is going on.

If this sort of thing is repeated over and over with different status
codes, that should be simplified into a single method, passing in the
code.

Thanks Tamara you made me realize that short syntax is not always best.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs