-1

I am trying to render a view from the current one using jquery. I am using flexigrid for my project and I want to code for the info button.

Here is my view. Using jquery I am calling the following,

var name = $('.trSelected .sorted div').html();
$.post("/contacts/display_info/", {file_as : name});

My controller is the following. Tried debugging also.

def display_info
        @contact = Contacts.find_by_file_as(params[:file_as])
        puts "---------------"
        puts @contact.id
        puts "---------------"
        render(:action => 'display_info' , :id => @contact.id)
    end

And here is my console:

enter image description here

Cant figure out what is the problem. Please help!

Strife
  • 243
  • 7
  • 22
  • why don't you use `redirect_to contact_path(@contact)` instead of `render(:action => 'contacts' , :id => @contact.id)` – rony36 Mar 07 '14 at 05:02
  • @rony36 It still show that '500 internal server error'. – Strife Mar 07 '14 at 05:05
  • is there any view for `contact` action? – rony36 Mar 07 '14 at 05:07
  • @rony36 oops!! I edited the question. Now its showing rendered 'contacts/display_info'. I edited the question – Strife Mar 07 '14 at 05:15
  • errr, what's the problem? You're posting to an action, and the log shows the view for that action getting rendered. – sevenseacat Mar 07 '14 at 05:30
  • @sevenseacat The problem is it is not getting rendered in the browser. – Strife Mar 07 '14 at 05:36
  • We could see CSRF issue in your log. Just see, if any of these links helps. http://stackoverflow.com/questions/11986939/cant-verify-csrf-token-authenticity-in-rails, http://stackoverflow.com/questions/7203304/warning-cant-verify-csrf-token-authenticity-rails, http://stackoverflow.com/questions/14039475/rails-3-cant-verify-csrf-token-authenticity, http://stackoverflow.com/questions/21273854/rails-ajax-cant-verify-csrf-token-authenticity – Rajesh Omanakuttan Mar 07 '14 at 06:01
  • @RajeshCO he is saying it renders in the rails console but it isnt going thru in the WEB BROWSER..kind of strange – Standin.Wolf Mar 07 '14 at 06:27

3 Answers3

0
  1. If you are doing a ajax request it should prefferably be JSON.
  2. For Json, check for a file named contacts.json.erb in contacts folder.
  3. You can try using remote true, much clean and better option than ajax/json requests.

Check this : http://railscasts.com/episodes/205-unobtrusive-javascript

try putting in your controller

respond_to do |format|
    format.html
    format.json { render :json=> {:success => true, :id => @contact.id } }
end

Something like this.

Sahil Grover
  • 1,862
  • 4
  • 26
  • 57
0

in your layout, please add:

<%= csrf_meta_tag %>

and when you are posting make sure you are sending:

 "authenticity_token": "<%= form_authenticity_token %>"

more specifically:

$(document).ajaxSend(function(e, xhr, options) {
  var token = $("meta[name='csrf-token']").attr("content");
  xhr.setRequestHeader("X-CSRF-Token", token);
});

Thanks.

rony36
  • 3,277
  • 1
  • 30
  • 42
0

The problem is that you're making an ajax request, but you're not doing anything with it. You need a callback that will capture the response from the request, and process it in some way.

Something like:

$.post("/contacts/display_info/", {file_as : name}, function(data) {
  // `data` will be the HTML returned by the server

  // If for example you want to render this HTML on your page, you may want something like: 
  $('#my_fancy_div').html(data);
});

You will also want to change your controller action to be render layout: false, presuming you only want to render the HTML in the view, and not the surrounding application layout.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
  • I want to move the control to '/contacts/display_info/name_of_the_contact'. How do I do that. I am sorry if my question is wrong but can it be done without ajax?? – Strife Mar 07 '14 at 05:49
  • @sevenseacat he is saying it renders in the rails console but it isnt going thru in the WEB BROWSER..kind of strange – Standin.Wolf Mar 07 '14 at 06:27
  • @sevenseacat I got the answer. it was window.location.replace. And i was goin in completely the wrong direction. Thanks you for assisting! – Strife Mar 07 '14 at 07:20