Ruby on rails Rails:未定义的方法&x27;(型号名称)和#x27;?
嗨,我现在正在制作一个上传图片的网络应用程序。有用户、相册和照片。当我试图编辑用户的个人资料信息时,我很难弄清楚发生了什么。它告诉我在UsersController更新中有一个未定义的方法'album'NoMethodError,但我在更新操作中甚至没有调用albumRuby on rails Rails:未定义的方法&x27;(型号名称)和#x27;?,ruby-on-rails,Ruby On Rails,嗨,我现在正在制作一个上传图片的网络应用程序。有用户、相册和照片。当我试图编辑用户的个人资料信息时,我很难弄清楚发生了什么。它告诉我在UsersController更新中有一个未定义的方法'album'NoMethodError,但我在更新操作中甚至没有调用album def update respond_to do |format| if current_user.update_attributes(params[:user]) format.html { redire
def update
respond_to do |format|
if current_user.update_attributes(params[:user])
format.html { redirect_to current_user, notice: 'User successfully updated.'}
format.json { render json: current_user, status: :created, location: current_user }
else
format.html { render action: 'edit' }
format.json { render json: current_user.errors, status: :unprocessable_entity }
end
end
end
错误具体出现在我的更新操作的第2行和第3行。以下是错误:
app/controllers/users\u controller.rb:45:in'block in update'
app/controllers/users\u controller.rb:44:in
update'`
有人知道到底发生了什么吗
为用户编辑表单
<%= form_for(@user) do |f| %>
<div class="formhold">
<div class="field">
<% if @user.profilepic? %>
<%= image_tag @user.profilepic.url %>
<% end %>
<%= f.label :profilepic, "Profile Picture" %>
<%= f.file_field :profilepic %>
</div>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div>
<%= f.submit 'Submit', :class => 'submitbutton' %>
</div>
<% if @user.errors.any? %>
<div id="error_exp">
<h2><%= pluralize(@user.errors.count, "error") %> occurred.</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<% end %>
<%= link_to "Back", user_path(@user) %>
'提交按钮'%>
发生。
用户模型
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :name, :password, :password_confirmation, :profilepic
validates_presence_of :password, :on => :create
validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_length_of :password, :minimum => 5, :on => :create
validates :album, :uniqueness => true
has_many :album_users
has_many :albums, :through => :album_users
accepts_nested_attributes_for :albums
has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
has_attached_file :profilepic
before_save { |user| user.email = email.downcase }
def name_with_initial
"#{name}"
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
class用户:create
验证以下内容的格式:name,:with=>/[A-Za-z]+/,:on=>:create
验证以下内容的格式:电子邮件,:with=>/\A([^@\s]+)@((?:[-A-z0-9]+\)+[A-z]{2,})\z/i,:on=>:create
验证以下内容的长度:password,:minimum=>5,:on=>:create
验证:相册,:唯一性=>true
有很多:相册用户
拥有\u多个:相册,:至=>:相册\u用户
接受相册的\u嵌套\u属性\u
有很多:友谊,:依赖=>:毁灭
有很多:朋友,:通过=>:友谊,:条件=>“状态='接受'”
有很多:请求的朋友,:通过=>:friends,:source=>:friends,:conditions=>“status='requested'”,:order=>:created\u at
有很多:待定的朋友,:通过=>:friends,:source=>:friends,:conditions=>“status='pending'”,:order=>:创建于
已附加文件:profilepic
在保存之前{| user | user.email=email.downcase}
def名称_,带_首字母
“#{name}”
结束
私有的
def创建\u记住\u标记
self.memory_token=SecureRandom.urlsafe_base64
结束
结束尝试注释:
validates :album, :uniqueness => true
假设:album是用户的属性/方法,但失败了。根据业务逻辑,您应该在相册模型中处理相册的唯一验证。您可以基于
user\u id
或其他具有:scope=>[:user\u id]
的属性来验证它们?另外,用户模型上是否有回调?保存前、保存后等。让我快速查看一下,您是否有正在更新的表单中的相册参考?您可能需要在您的模型中通过accepts_nested_attributes_处理该问题,因为我刚刚上传了用户编辑表单和用户模型。我现在就去看看它成功了!!!但是你能告诉我为什么吗?我不知道相册的唯一性是如何与我的表单相关联的,以获取用户信息的?您试图验证/阻止什么?用户只能有一个相册?我想我明白了。这只是因为当我胡闹着看我的应用程序是否有任何错误时,它看起来像是同一张专辑被创建了多次。谢谢