0

I am trying to display response from the rails server in a div.

ajax call is like this

$.get('/news/search?search=' + existingString, function(data) {
  $("#results").html("");
  console.log(data);
  len=data.length;
  $('#result').append('#{escape_javascript(render("newsdisplay") )}');

_newsdisplay.html.haml

- if @results
  - @results.each do |anzen|
    %li.clearfix.postMain.allnews
     .wrapper
       %li
        .messaIcon
          .icon
            %img{:src => anzen.news_image.thumb.url}
        .messaCon
          .areaDay.clearfix
            %p.messaTextCon
            グループ名:
            %span= anzen.news_name

search action

def search
  if params[:search].present?
    @results = News.search(params[:search])
  end
end

routes.rb

get 'news/search', to: 'news#search', as: :newssearch

I am getting the response from the server correctly. But no change in the page.But in console the full page with rendered response is displayed. Can anyone guess the issue

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Sachin
  • 135
  • 9

2 Answers2

0

The first code snippet is plain js, right? You cannot render the newsdisplay inline JS, the HTML has to be rendered before, so you should have a search.js.erb as a view that just renders the newsdisplay view.

The this example:

How can I render a partial from .js.erb file in rails 4?

Community
  • 1
  • 1
JaschaL
  • 81
  • 7
0

Try this

$('#result').append("<%= escape_javascript(render(:partial => 'newsdisplay') )");
Preethi Mano
  • 445
  • 1
  • 7
  • 18
  • You are making a ajax call so your response should be either js or json.so change your search.html.haml file to search.js.erb @Akhil Sudhakaran – Preethi Mano Aug 22 '16 at 10:55