简体   繁体   中英

How to return html without layout in Ruby on Rails

This is javascript

$('#course_name, #subject').on('input', () => {
    $.ajax({
        url: '/search_courses_list',
        method: 'get',
        data: {
            course: {name: $('#course_name').val()},
            subject: $('#subject').val()
        },
        success: function (data) {
            $('#course_search_result').html(data)
        }
    })
}).trigger('input')

This is the controller method.

def search_courses_list
@courses = Course.joins(:segments).where('lower(name) like :str', str: "%#{params["course"]["name"].downcase}%")
if (params["subject"]!="")
  @courses = @courses.where({segments: {subject_id: params["subject"]}})
end
render :search_result, courses: @courses
end

def search_result
  @courses = courses
end

I tried to use ajax to directly get the Erb generated text as Html strings and insert them directly into the container I want. Is that possible that I get only the Erb file I want without surrounded by the layout?

https://guides.rubyonrails.org/layouts_and_rendering.html#the-layout-option

You can do this on your action

render layout: false

您可以简单地将render :search_result, courses: @coursesrender :search_result, courses: @courses, layout: false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM