gpt4 book ai didi

ruby-on-rails - # 的 MembersController#index 未定义方法 `total_pages' 中的 NoMethodError

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:57 26 4
gpt4 key购买 nike

错误输出为:

NoMethodError in MembersController#index
undefined method `total_pages' for #<Micropost::ActiveRecord_Associations_CollectionProxy:0x007ffc26104288>

应用程序跟踪是:

app/views/shared/_feed.html.erb:5:in `_app_views_shared__feed_html_erb__933429040368905157_70360748756160'
app/views/members/index.html.erb:4:in `_app_views_members_index_html_erb__2919315027143638402_70360970104820'

更新:

源接近错误:

        arel.public_send(method, *args, &block)
else
super # marked red
end
end
end

附加信息:

app/views/members/index.html.erb 使用另一个 Controller microposts_controller 呈现两个部分。因此,我将展示两个 Controller 和两个部分的内容。

# app/views/members/index.html.erb
<%= render 'shared/micropost_form', :locals => { :micropost => @micropost } %>
<%= render 'shared/feed', :locals => { :feed_items => @feed_items } %>

成员 Controller

class MembersController < ApplicationController
def index
@micropost = current_user.microposts.build
@feed_items = current_user.microposts
end
end

微博 Controller .rb

# controllers/micropost_controller.rb

class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy

def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to '/members'
else
@feed_items = []
render '/members'
end
end

def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || '/members'
end

private

def micropost_params
params.require(:micropost).permit(:content, :picture)
end

def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to '/members' if @micropost.nil?
end
end

_micropost_form.html.erb

# shared/_micropost_form.html.erb

<%= form_for(@micropost, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost (420 chars max)..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>

<script type="text/javascript">
$('#micropost_picture').bind('change', function() {
size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 5) {
alert('Maximum file size is 5MB. Please choose a smaller file.');
}
});
</script>

_feed.html.erb

# shared/_feed.html.erb

<% if @feed_items.any? %>
<ol class="microposts">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>

最佳答案

您想在 View 中使用 will_paginate 对记录进行分页,但您没有在 Controller 中对它们进行分页:

def index
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end

关于ruby-on-rails - #<Micropost::ActiveRecord_Associations_CollectionProxy:0x007ffc26104288> 的 MembersController#index 未定义方法 `total_pages' 中的 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com