0

My code in controller looks like this:

def dashboard
    @proj_users.each do |us|
      @users<<User.find(us.user_id).email
    end
    render :json=>@users
end

In my view page the javascript looks like this:

$(".clicked").click(function () {
  $.get("/dashboard", {
      clicked_id:this.id,
      user:$("#user").val()
    },
    function (data) {
      $("#all_users").hide();
      $("#proj_users").html(data);
    });
});

@users is an array containing email id. as you can see the data will have @users.

@users will be something like this [email protected],[email protected]

$("#proj_users").html(data); is not getting updated with data..

Kindly help me..

1
  • I changed render:json to render :text, now the div gets updated with the data.But data is displayed as "[email protected],[email protected]".I want it to display line by line.. Commented Nov 3, 2012 at 6:21

2 Answers 2

1
two ways

1) solution 1

  @proj_users.each do |us|
     email  = User.find(us.user_id).email + "<br>"
     @users << email
   end
   render :text=>@users

2) solution 2

  @proj_users.each do |us|
     @users<<User.find(us.user_id).email
   end
   render :json=>@users

   in view

   $(".clicked").click(function(){
        $.get("/dashboard",{
        clicked_id:this.id,
        user:$("#user").val()
        },
        function(data)
        {
         $("#all_users").hide();
         $.each(data, function(email){
           $("#proj_users").append(email + "<br>");
         }
        });
   });
Sign up to request clarification or add additional context in comments.

Comments

1

You can simplify the iteration:

def dashboard
  render :json => @proj_users.map(&:email)
end

Now you need to iterate (using .each() method) on the front end and put this in your html as shown by @ubaid's callback function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.