当我为:photo字段选择新值并更新时,我的更新表单给了我这个错误。当更新和保存用户名,手机或电子邮件等非图像字段时,它不会出错。我正在使用paperclip作为文件附件管理宝石。
end
attr_accessible :photo # this is where the error points to
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
这里是完整的跟踪:http://pastie.org/private/etie0qfqujkvrzpetpuqw
这是edit.html.erb:
<h1>Add A teacher</h1>
<%= form_for @teacher, :url => { :action => 'update', :id => @teacher.id}, :html => { :multipart => true } do |f| %>
<table summary="teacher form fields">
<tr>
<th>First Name*</th>
<td><%= f.text_field :firstname %></td>
</tr>
<tr>
<th>Last Name*</th>
<td><%= f.text_field :lastname %></td>
</tr>
<tr>
<th>Email*</th>
<td><%= f.email_field :email %></td>
</tr>
<tr>
<th>Cellphone</th>
<td><%= f.telephone_field :cellphone %></td>
</tr>
<tr>
<th>Username*</th>
<td><%= f.text_field :username %></td>
</tr>
<tr>
<tr>
<th>Password*</th>
<td><%= f.password_field :password %></td>
</tr>
<tr>
<th>Confirm Password*</th>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<tr>
<th>Address Street#</th>
<td><%= f.text_field :addr_streetno %></td>
<th>Apt #</th>
<td><%= f.number_field :addr_aptno %></td>
</tr>
<tr>
<th>City</th>
<td><%= f.text_field :addr_city %></td>
<th>State</th>
<td><%= f.text_field :addr_state %></td>
<th>Zip</th>
<td><%= f.number_field :addr_zip %></td>
</tr>
<tr>
<th>Photo</th>
<td><%= f.file_field :photo %></td>
</tr>
</table>
<%= f.submit 'Update teacher' %>
<% end %>
<% if @teacher.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in @teacher.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>
这是控制器:
class TeacherController < ApplicationController
def index
@teachers= Teacher.all
end
def new
@teacher = Teacher.new
end
def create
@teacher = Teacher.new(teacher_params)
if @teacher.save
flash[:notice] = "Teacher created."
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def edit
@teacher = Teacher.find(params[:id])
end
def update
@teacher = Teacher.find(params[:id])
# Update the object
if @teacher.update_attributes(teacher_params)
# If update succeeds, redirect to the list action
flash[:notice] = "Teacher updated."
redirect_to :action => 'show', :id => @teacher.id
else
# If update fails, redisplay the form so user can fix problems
render :action => 'edit'
end
end
def show
@teacher = Teacher.find(params[:id])
end
private
def teacher_params
params.require(:teacher).permit(:firstname,
:lastname,
:email,
:cellphone,
:username,
:password,
:password_confirmation,
:addr_streetno,
:addr_aptno,
:addr_city,
:addr_state,
:addr_zip,
:photo)
end
end
以下是模型:
class Teacher < ActiveRecord::Base
has_many :students
has_secure_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
CELLPHONE_REGEX = /\A([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})\z/i
validates :firstname, :presence => true
validates :lastname, :presence => true
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :cellphone, :presence => true, :format => CELLPHONE_REGEX
[:addr_aptno, :addr_zip].each do |n|
validates n, numericality: { only_integer: true,
:greater_than => 0,
:allow_blank => true
}
end
attr_accessible :photo
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" }
end
答案 0 :(得分:3)
回答原始问题
更新图像字段会给出NoMethodError未定义的方法
name' for nil:NilClass in
update'
OP没有使用amy gem来帮助上传导致此问题的Rails应用程序中的文件。建议使用其中一个众所周知的宝石 Paperclip 或 CarrierWave 。
回答修改后的问题
使用回形针进行更新时,未定义的方法`attr_accessible' 更新表格中的照片字段
当您使用Rails 4
时,attr_accessible :photo
将无效,因为它已被删除。相反,您需要在Strong Parameters
方法中允许:photo
属性,将teacher_params
用于属性白名单。
因此,您只需从attr_accessible :photo
模型中删除Teacher
。
答案 1 :(得分:0)
添加重新启动rails服务器(因为添加了回形针gem)&amp;将add_attachment:,:添加到迁移文件并运行它,表中的以下字段(add_attachment:老师:照片用于在这里运行)
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
此外,我不得不在控制器文件中的强参数分配调用中添加:photo,并且还必须在迁移文件&amp;中添加add_attachment:teachers:photo。运行。有几件事。