gpt4 book ai didi

ruby-on-rails - # 的未定义方法 `avatar'

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:22 25 4
gpt4 key购买 nike

我正在尝试显示按名字或姓氏搜索的用户的数据。我能够显示为用户返回的所有数据(例如,如果按名字搜索用户,我可以提取与搜索到的用户关联的所有其他字段/数据,例如城市、州/省等)。

似乎给我一个错误的唯一字段是与每个用户关联的头像(个人资料照片)字段。 <%= image_tag @user.avatar.url(:thumb) %>

注意:我可以从其他用户页面(例如 show.html.erb)显示此字段。

我在点击搜索结果页面时遇到的错误是:

NoMethodError in Users#index - undefined method `avatar' for # User::ActiveRecord_Relation:0x007fc328248ef0

_user.html.erb

<div class="card">

<div class="columns">

<div class="col">

<%= image_tag @user.avatar.url(:thumb) %>


</div>

<div class="col">

<div class="name">
<%= user.firstname %> <%= user.lastname%>

</div>
<br>
<b><%= user.city%>, <%= user.stateprov%></b>

</div>

</div>

</div>

index.html.erb

<% if @user.present? %>
<%= render @user %>
<% else %>
<p>There are no posts containing the term(s) <%= params[:search] %>.</p>
<% end %>

用户.rb

class User < ActiveRecord::Base

def self.search(search)
where("firstname LIKE ? OR lastname LIKE ?", "%#{search}%", "%#{search}%")
end


has_secure_password

validates_length_of :password, :in => 6..20, :on => :create
validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] }

has_attached_file :avatar,


:path => ":rails_root/public/system/:attachment/:id/:basename_:style.:extension",
:url => "/system/:attachment/:id/:basename_:style.:extension",


:styles => {
:thumb => ['175x175#', :jpg, :quality => 100],
:preview => ['480x480#', :jpg, :quality => 70],
:large => ['600>', :jpg, :quality => 70],
:retina => ['1200>', :jpg, :quality => 30]
},
:convert_options => {
:thumb => '-set colorspace sRGB -strip',
:preview => '-set colorspace sRGB -strip',
:large => '-set colorspace sRGB -strip',
:retina => '-set colorspace sRGB -strip -sharpen 0x0.5'
}

validates_attachment :avatar,
:presence => true,
:size => { :in => 0..10.megabytes },
:content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }


end

users_controller.rb

class UsersController < ApplicationController

def create
@user = User.new(user_params)
if @user.save
flash[:success] = "You signed up successfully"
flash[:color] = "valid"
redirect_to @user
else
flash[:notice] = "Form is invalid"
flash[:color] = "invalid"
render "new"
end
end


def index
@user = User.all
if params[:search]
@user = User.search(params[:search]).order("created_at DESC")
else
@user = User.all.order('created_at DESC')
end
end



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

def edit
@user = User.find(params[:id])
end

def update
@user = User.find(params[:id])

if @user.update_attributes(user_params)

redirect_to @user

else

render 'edit'
end
end


private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :aptno, :streetaddress, :city, :country, :stateprov, :poszip, :receive_newsletters, :terms_accepted, :password, :password_confirmation, :avatar)
end

end

最佳答案

您正在尝试将一组用户用作单个用户。

def index
@users = User.all
if params[:search]
@users = User.search(params[:search]).order("created_at DESC")
else
@users = User.all.order('created_at DESC')
end
end

<% if @users.any? %>
<%= render @users %>
<% else %>
<p>There are no posts containing the term(s) <%= params[:search] %>.</p>
<% end %>

到目前为止,所做的更改只是为了避免开发人员混淆。然而,真正的症结在于您使用 @user 而不是 user 的部分。

<%= image_tag @user.avatar.url(:thumb) %>

此处的区别在于 user 是 Rails 在渲染部分时创建的局部变量。而 @user 是您的未命名集合!

所以只用局部变量:

<%= image_tag user.avatar.url(:thumb) %>

并在命名变量时注意复数形式。

关于ruby-on-rails - #<User::ActiveRecord_Relation:0x007fc328248ef0> 的未定义方法 `avatar',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35469817/

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