开发者

rails-json-jquery not showing view file contents

开发者 https://www.devze.com 2023-03-01 05:35 出处:网络
i am a beginner with jquery. I have a call to a controller fromas below: jQuery(\'#search_form\').submit(function(){

i am a beginner with jquery.

I have a call to a controller from as below:

jQuery('#search_form').submit(function(){
        jQuery.post("topics/topics_details", {
            'topic_name': jQuery('#topic_name').val(),
        }, function(response){
        }, 'json');

         alert("hello");
         return false;
     });

Then,from inside function i perform some instructions.

def topics_details
    @ta开发者_如何转开发g=params[:topic_name]
    puts "\n\nTopic = ",@tag
  end 

then, i need to execute the view file(topics_details.html.erb).But, unfortunately that view is not showing(but,in console that is rendered.),instead the alert is showing.

How to call a partial file?render_to_string/json can u give a basic example...

Can anybody ,please help... thanks in advance...


All sorts of problems here and it's hard to know what you're actually trying to do. Your alert is outside of the jQuery.post call so it will always happen. Why is that alert there? You don't do anything with the response, so you need to either: a) put something in the function(response) block or b) remove it and let rails deal with the response, eg in a topics_details.js.rjs file in the app/views/topics folder. Typically this would replace the contents of a div on the page with a particular partial. You don't 'execute the view file' as such, but rather evaluate it and replace some content on the page.

Rather than catch the form submittal, and send an ajax request, why not just make a it a remote form? That way it will submit using ajax automatically, and your controller will deal with it in the normal way, then respond with its .js format block rather than its .html format block. By default this will look for a file called .js.rjs in the relevant view folder. eg

<%=  form_remote_tag :url => '/topics/topics_details' do %>
  usual form stuff here - fields, submit etc
<% end %>

This will make an ajax request to /topics/topics_details. This does what it does normally, eg it could be to refresh a variable called @topics which holds an array of Topic objects. These could be listed on your page in a div with an id of 'topics'. So, you just need to refresh that div. Make sure that this div lives in a partial called '_topics.html.erb' in the topics folder and you can then just have the following js.rjs file:

#in app/views/topics/topics_details.js.rjs
@page.replace "topics", :partial => "topics/topics.html.erb"
0

精彩评论

暂无评论...
验证码 换一张
取 消