Rails: rendering partial elements via Ajax using buttons

I currently have a page that displays a list of microposts, and the page also includes several tabs, such as Most Recent Most Discussed Most Viewed . I was wondering what is the best way to display different types of order lists on a microcell without refreshing the entire page and through ajax. I'm not sure how to do this, but I want to find out if someone helped me help. Thanks:). I have so many so far.

HTML page

 <div id='ContentHeader'> <%= render 'microposts/new' %> <div class='StreamTabContainer'> <ul class='StreamTabs'> <li class='StreamTab StreamTabRecent active'> <%= link_to 'Most Recent', some_path, :remote => true, :class => 'TabText' %> </li> <li class='StreamTab StreamTabDiscussed'> <%= link_to 'Most Discussed', some_path, :remote => true, :class => 'TabText' %> </li> <li class='StreamTab StreamTabViews '> <%= link_to 'Most Views', some_path, :remote => true, :class => 'TabText' %> </li> <li class='StreamTab StreamTabRated'> <%= link_to 'Highest Rated', some_path, :remote => true, :class => 'TabText' %> </li> </ul> </div> </div> <div id='ContentBody'> <div id='ajax'></div> <%= render 'users/microposts', :microposts => @microposts %> </div> 
+4
source share
1 answer

You seem to be on the right track. Your opinion seems true with a quick look.

There are just a few final parts to this job, starting with the fact that your controller can accept js calls:

  # Inside Controller action respond_to do |format| format.js end 

Once you have configured this, you need to create javascript to return to the client for each call. Create files in the view folder

  # app/views/controllerName/actionName.js.erb $('#ajax').html("<%= escape_javascript(render(:partial => 'content', :sortingType => 'type')) %>"); 

The above will allow you to display a partial that shows the messages (in this example I named this content), and pass partial to the sortingType local variable. This answer will be posted inside the html div id = "ajax" (from your example). The last step is to create a partial view that will make all your posts sorted the way you would like

 # app/views/resourceName/_content.html.erb # Simply create your view partial here, which uses the :sortingType to display your posts in the order you need 

I hope this brings you closer to your intentions.

Edit: implemented I had some problems with my quotes.

+5
source

Source: https://habr.com/ru/post/1399432/


All Articles