1

I am working on a scenario where a <div> tag is refreshed using ajax whenever a user clicks on row of table. But due to some issue, it is always getting "alert(fail)" message. When i replace view file with just "HI" text, it works.. but when having below mentioned form, it shows fail alert. Please help.

.JS file

$(document).ready(function(){
    $('.tab tr').click(function(){
        var val= $(this).attr('id');
        alert(val);
    $.ajax({
           url: "/users/profile/",
           type: "GET",
           data: {'id': val},
           success: function(response){
              $(".tab").html(response) 
            },
           error: function(response) {
                        alert('fail');
                      },
});

   });
    });

My controller method :

def profile 
@prof = User.where(:id => params[:id])

respond_to do |format|
format.html {}
end

Method's view:

<%= simple_form_for(@prof, :html => {:class => 'form-vertical'}) do |f| %>
<%= f.error_notification %>
<%= f.first_name %>
<%end%>
8
  • what happens when you try format.js instead of format.html? Commented Mar 30, 2014 at 18:54
  • its still coming the same way ..i tried it by $(".tab").html(response) and in controller method .. respond_to do |format| format.html {} format.js{} end Commented Mar 30, 2014 at 18:57
  • 1
    Please look into log and if you can't solve problem by yourself post it here. Commented Mar 30, 2014 at 18:58
  • hi michael .. i have tried for long by myself. As i could not figure out so i have posted it here Commented Mar 30, 2014 at 19:00
  • Anything in the logs? Commented Mar 30, 2014 at 19:04

2 Answers 2

1

The where method in the following code returns an array:

@prof = User.where(:id => params[:id])

However in your view, you are referring to a single item:

<%= f.first_name %>

Replace the where method in the controller with find, which returns a single item:

@prof = User.find(params[:id])
Sign up to request clarification or add additional context in comments.

Comments

0

I think your url should be as

url: "/users/profile"

not

url: "/users/profile/"

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.