简体   繁体   English

Rails部分渲染在控制台网络响应中但不在视图中

[英]Rails partial renders in console network response but not in view

I am trying to have my partial display search results in a rails view. 我试图在Rails视图中显示部分显示搜索结果。 When I check my console > network > preview, I see my partial rendered with the data I need but it doesn't display in my view. 当我检查控制台>网络>预览时,我看到部分渲染了所需的数据,但未在视图中显示。

Here's my app/controllers/searchs_controller.rb 这是我的app / controllers / searchs_controller.rb

def nearby_guides
@address = params[:address]
initial_array = @address.split(',')
city = initial_array[0]
state = initial_array[1].split(' ')
final_state = state[0]
country = initial_array[2]
@result = User.where("city = ?", city)
respond_to do |format|
    format.html {render :partial => '/searchs/result', object: @result}
end
end

Here's my views/searchs/_result.html.erb 这是我的views / searchs / _result.html.erb

<h1>HELLO WORLD</h1>

<% if @result %>
    <% @result.each do |r| %>
        <p><%= r.firstname %></p>
    <% end %>
<% end %>

Then in my views/searchs/new.html.erb 然后在我的views / searchs / new.html.erb中

<%= render 'result' %>

This is the response I see in my console > network > preview 这是我在控制台>网络>预览中看到的响应

HELLO WORLD

Kevin

Which is exactly the response I expected but again, it doesn't display in the view. 正是我所期望的响应,但是同样,它不会显示在视图中。 I've looked over stack overflow with no luck. 我看过堆栈溢出没有运气。 Thanks so much for any and all help! 非常感谢您提供的所有帮助!

I think that this is your issue: format.html {render :partial => 我认为这是您的问题: format.html {render :partial =>

When you are rendering the result of an action as html - it needs to be a whole view, not just a partial. 将动作结果呈现为html时-它需要是一个整体视图,而不仅仅是局部视图。

Rendering partials is generally reserved for AJAX queries - where you need only part of the page and your javascript will insert it into the page in the right place. 呈现部分内容通常保留给AJAX查询-您只需要页面的一部分,而javascript会将其插入到页面的正确位置。 so either you need javascript, or you need to do something like: render :new 所以要么需要JavaScript,要么需要执行以下操作: render :new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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