在Rails App中通过Ajax呈现为部分

时间:2022-06-01 06:55:28

I have almost done! but I have an issue, in my Controller file have this:

我差不多完成了!但我有一个问题,在我的Controller文件中有这个:

def show
   @user = User.find(params[:id])
   @posts = @user.posts.paginate(page: params[:page])
end

Then I have this piece of code in my file show.html.erb:

然后我在我的文件show.html.erb中有这段代码:

<div class="span8">
   <%= render 'follow_form' if signed_in? %>
 <% if @user.posts.any? %>
    <h3>Microposts (<%= @user.posts.count %>)</h3>
    <div id='posts'>
      <div class='page'>
     <ol class="microposts">
        <%= render @posts %>
     </ol>
  </div>
</div>
   <% end %>
 </div>

At the bottom of this file, I have a Javascript code that I have taken from the tutorial: https://github.com/amatsuda/kaminari/wiki/How-To:-Create-Infinite-Scrolling-with-jQuery

在这个文件的底部,我有一个Javascript代码,我从教程中获取:https://github.com/amatsuda/kaminari/wiki/How-To:-Create-Infinite-Scrolling-with-jQuery

In the same folder I have the file index.js.erb with:

在同一个文件夹中,我有index.js.erb文件:

$("#articles").append("<div class='page'><%= escape_javascript(render(@users)) %></div>");
$("#posts").append("<div class='page'><%= escape_javascript(render(@posts)) %></div>");

In a partial _posts.html.erb have this:

在部分_posts.html.erb中有这样的:

<div class='article'>
  <%= image_tag(user.picture_url, :width => 50) %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                              data: { confirm: "You sure?" } %>
  <% end %>
</div>

The first one already works in my file index.html.erb, the problem is with the second piece of code, when I try to render the partial at @post, It brings the follow log:

第一个已经在我的文件index.html.erb中工作,问题是第二段代码,当我尝试在@post渲染局部时,它带来了以下日志:

**
'nil' is not an ActiveModel-compatible object that returns a valid partial path.
Extracted source (around line #2):
1: $("#articles").append("<div class='page'><%= escape_javascript(render(@users)) %></div>");
2: $("#posts").append("<div class='page'><%= escape_javascript(render(@posts)) %></div>");
**

How can I render that partial?

我该如何渲染那部分?

Thanks a lot :D

非常感谢:D

2 个解决方案

#1


1  

I usually use the .js.erb template to render the partial into a var then set it on the page using JS. Be sure to escape_javascript the template content otherwise you will get JS errors.

我通常使用.js.erb模板将部分渲染为var,然后使用JS在页面上设置它。一定要rpm_javascript模板内容,否则你会得到JS错误。

<% template_html = render :partial => '_feed_item.html.erb' %>
<%="$('div#content').html('#{escape_javascript(template_html)}</div>');" %>

I think the above should work in erb. I use HAML so mine looks like:

我认为以上内容应该在erb中起作用。我使用HAML所以我看起来像:

-template_html = render :partial => 'partial_template'
!="$('div#content').html('#{escape_javascript(template_html)');"

Cheers.

干杯。

#2


0  

Based on your In script I'm calling to /users?page=, and I was wondering that It may calls the line, you must make sure that your controller is populating the @posts variable if your view is using this. Can you show the controller method users?

基于你的脚本我打电话给/ users?page =,我想知道它可能会调用该行,你必须确保你的控制器正在填充@posts变量,如果你的视图正在使用它。你能告诉控制器方法用户吗?

#1


1  

I usually use the .js.erb template to render the partial into a var then set it on the page using JS. Be sure to escape_javascript the template content otherwise you will get JS errors.

我通常使用.js.erb模板将部分渲染为var,然后使用JS在页面上设置它。一定要rpm_javascript模板内容,否则你会得到JS错误。

<% template_html = render :partial => '_feed_item.html.erb' %>
<%="$('div#content').html('#{escape_javascript(template_html)}</div>');" %>

I think the above should work in erb. I use HAML so mine looks like:

我认为以上内容应该在erb中起作用。我使用HAML所以我看起来像:

-template_html = render :partial => 'partial_template'
!="$('div#content').html('#{escape_javascript(template_html)');"

Cheers.

干杯。

#2


0  

Based on your In script I'm calling to /users?page=, and I was wondering that It may calls the line, you must make sure that your controller is populating the @posts variable if your view is using this. Can you show the controller method users?

基于你的脚本我打电话给/ users?page =,我想知道它可能会调用该行,你必须确保你的控制器正在填充@posts变量,如果你的视图正在使用它。你能告诉控制器方法用户吗?