我的网站上有一个作者模型,用户可以上传自己的头像。初始注册/图片上传有效,但是当我尝试使用作者#edit action更改图像后,我收到此错误:
param not found: author
params.require(:author).permit(:first_name, :last_name, :email, :password, :password_confirmation, :image)
author.rb
class Author < ActiveRecord::Base
before_create { generate_token(:auth_token) }
has_secure_password
before_save { email.downcase! }
mount_uploader :image, ImageUploader
has_many :novels
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email,
presence: true,
format: { with: VALID_EMAIL_REGEX, message: 'Invalid email address' },
uniqueness: { case_sensitive: false }
validates :first_name, presence: true
validates :last_name, presence: true
validates :password, presence: true, confirmation: true, on: :create
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
PasswordResetMailer.password_reset(self).deliver
end
def send_activation
AuthorMailer.activate(self).deliver
end
def generate_token(column)
self[column] = SecureRandom.urlsafe_base64
end
end
authors_controller.rb
def edit
@author = Author.find(params[:id])
end
def update
if @author.update(author_params)
flash[:notice] = "Profile has been updated."
redirect_to @author
else
flash[:alert] = "Profile has not been updated."
render :edit
end
end
private
def author_params
params.require(:author).permit(:first_name, :last_name, :email, :password, :password_confirmation, :image)
end
edit.slim
- provide(:title, 'Edit Author')
.row
.col-lg-4.col-lg-offset-1
h1 Edit Your Profile
hr
.row
.col-xs-6.col-lg-4
= simple_form_for @author, as: 'user_horizontal', html: { class: 'form-horizontal' }, wrapper: :horizontal_form, wrapper_mappings: { check_boxes: :horizontal_radio_and_checkboxes, radio_buttons: :horizontal_radio_and_checkboxes, file: :horizontal_file_input, boolean: :horizontal_boolean } do |f|
= image_tag @author.image_url, class: "avatar"
= f.input :image, as: :file
= f.submit "Submit", class: "btn btn-small btn-default"