-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!

8
  • why don't you use redirect_to contact_path(@contact) instead of render(:action => 'contacts' , :id => @contact.id) Commented Mar 7, 2014 at 5:02
  • @rony36 It still show that '500 internal server error'. Commented Mar 7, 2014 at 5:05
  • is there any view for contact action? Commented Mar 7, 2014 at 5:07
  • @rony36 oops!! I edited the question. Now its showing rendered 'contacts/display_info'. I edited the question Commented Mar 7, 2014 at 5:15
  • errr, what's the problem? You're posting to an action, and the log shows the view for that action getting rendered. Commented Mar 7, 2014 at 5:30

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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??
@sevenseacat he is saying it renders in the rails console but it isnt going thru in the WEB BROWSER..kind of strange
@sevenseacat I got the answer. it was window.location.replace. And i was goin in completely the wrong direction. Thanks you for assisting!
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.

3 Comments

I already have the <%= csrf_meta_tag %> in the 'head'. I cant figure out the place to add the second line. help.
I have mentioned it in the post.
I got the answer. it was window.location.replace. And i was goin in completely the wrong direction. Thanks you for assisting!
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.

6 Comments

No there is nothing of json in that folder.
@FredoCorleone wat happens if u type the web address u r rendering in the URL?
@sahil can you plz elaborate? maybe correct my code? It has to be through jquery because i am using flexigrid
I edited the question. My action was wrong. Please check it out.
Its 200 OK. What other problem are you facing ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.