3 Replies - 2407 Views - Last Post: 11 March 2013 - 01:24 PM Rate Topic: -----

#1 indyarocks  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 07-March 12

how to render json response on erb

Posted 08 March 2013 - 04:09 PM

Hi All,

I want to display error message on the view(erb file), when a controller renders a json of error.

This is the response I get when I do inspect element.
{"success":false,"err":"Please upload an image of the size 940x165 px or bigger in same height-width ratio in PNG, JPG, JPEG format."}


How can I show this message in erb file?

Thanks in advance,
Indy

This post has been edited by indyarocks: 08 March 2013 - 04:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to render json response on erb

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: how to render json response on erb

Posted 09 March 2013 - 01:11 AM

Can you explain a bit more what you're trying to do? Are you making an AJAX request? If so, what does your code look like? Where/how do you want to display the error message? What have you tried and in what way didn't it work?

Also: I assume this you're using Rails, right?

This post has been edited by sepp2k: 09 March 2013 - 01:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: how to render json response on erb

Posted 10 March 2013 - 07:01 AM

You will want to parse this result, I am writing this on PC so can not test the Ruby code but you could do something like:


string = "{"success":false,"err":"Please upload an image of the size 940x165 px or bigger in same height-width ratio in PNG, JPG, JPEG format."}"

result = JSON.parse string
if result.present? && parsed["success"] == "true"
 #was success
else
 #was fail, add each err to errors. 
end


This post has been edited by tycos: 10 March 2013 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: how to render json response on erb

Posted 11 March 2013 - 01:24 PM

You aren't all that clear in your post, but to get messages to the view from a controller, I usually use flash messages.

http://ruby.railstut...a_flash_message

so doing something like:

(your_controller.rb)
flash.now[:error] = 'This is an error message'



Will make a flash error available to the view. If you setup your view to include respond to these errors, then you will have a very convenient way of rendering errors on any arbitrary view.

(app/views/layouts/application.html.erb)
.
.
.
<%= render 'layouts/flash', :flash => flash %>
.
.
.



(app/views/layouts/_flash.html.erb)
<div id="page-wrap">
    <% flash.each do |key, value| %>
        <div class="flash <%= key %>"><%= value %></div>
    <% end %>
</div>




You should go through that tutorial if you haven't already, it's really helpful.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1