如何允许具有强参数的CarrierWave属性?

时间:2014-05-13 15:12:25

标签: ruby-on-rails carrierwave

我有一个带有CarrierWave上传器的模型:

author.rb

class Author < ActiveRecord::Base 
  before_create { generate_token(:auth_token) } 
  has_secure_password 
  before_save { email.downcase! } 

  mount_uploader :avatar, AvatarUploader 

  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

应用/视图/作者/ new.html.erb

<%= simple_form_for(@author, :defaults => { :wrapper_html => {:class => 'form-group'}, :input_html => { :class => 'form-control', :multipart => true } }) do |f| %>
    <%= f.input :first_name %>
    <%= f.input :last_name %>
    <%= f.input :email %>
    <%= f.input :avatar %>
    <%= f.input :password %>
    <%= f.input :password_confirmation, label: "Confirmation" %>

    <%= f.submit "Submit", class: "btn small btn-default" %> 
<% end %>

authors_controller.rb

class AuthorsController < ApplicationController
   @author = Author.new(author_params) # Line no 16

  if @author.save 
    AuthorMailer.activate(@author).deliver 
    flash[:notice] = "Please check your email to activate your account." 
    redirect_to root_path 
  else 
   render :new 
  end 

  private

   def author_params
      params.require(:author).permit(:first_name, :last_name, :email, :password, :password_confirmation, :avatar)
   end

当我尝试创建新作者时,我收到以下错误:

wrong number of arguments (1 for 2) at
@author = Author.new(author_params)

我认为像CarrierWave Uploader这样的强参数有一个特定的语法......

从聊天中的内容编辑

日志

Started POST "/authors" for 127.0.0.1 at 2014-05-13 11:04:22 -0500 
Processing by AuthorsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TCZ8etUWEvuiaaqXOtbhAXEaxvbjBgm6UhZFxnlFHfw=", "author"=>{"first_name"=>"Michael", "last_name"=>"Glaz", "email"=>"mikeglaz@yahoo.com", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x0000000606e7e8 @tempfile=#<Tempfile:/tmp/RackMultipart20140513-575-kelnso>, @original_filename="me.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"author[avatar]\"; filename=\"me.jpg\"\r\nContent-Type: image/jpeg\r\n">, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Submit"} 
Completed 500 Internal Server Error in 55ms 

ArgumentError (wrong number of arguments (1 for 2)): 
app/controllers/authors_controller.rb:16:in `create' 


Rendered /home/mglaz/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms) 
Rendered /home/mglaz/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.7ms) 
Rendered /home/mglaz/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms) 
Rendered /home/mglaz/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.0ms)

架构定义

class CreateAuthors < ActiveRecord::Migration 
  def change 
    create_table :authors do |t| 
      t.string :first_name 
      t.string :last_name 
      t.string :email 
      t.string :password_digest 
      t.boolean :admin 
      t.boolean :active 
      t.string :avatar 
      t.string :auth_token 
      t.string :password_reset_token 
      t.datetime :password_reset_sent_at 
      t.timestamps 
   end 
  end 
end

0 个答案:

没有答案