需要帮助设置回形针

发布于 2024-11-05 09:44:32 字数 2771 浏览 0 评论 0原文

我第一次尝试使用回形针。我尝试使用用户头像,但当我查看个人资料时,唯一显示的就是“缺失”一词。

class UsersController < ApplicationController
    # GET /users
    # GET /users.xml
    def index
        @users = User.all

        respond_to do |format|
            format.html # index.html.erb
            format.xml  { render :xml => @users }
        end
    end

    # GET /users/1
    # GET /users/1.xml
    def show
        @user = User.find(params[:id])

        respond_to do |format|
            format.html # show.html.erb
            format.xml  { render :xml => @user }
        end
    end

    # GET /users/new
    # GET /users/new.xml
    def new
        @user = User.new

        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @user }
        end
    end

    # GET /users/1/edit
    def edit
        @user = User.find(params[:id])
    end

    # POST /users
    # POST /users.xml
    def create
        @user = User.new(params[:user])

        respond_to do |format|
            if @user.save
                format.html { redirect_to(:users, :notice => 'Registration successfull.') }
                format.xml { render :xml => @user, :status => :created, :location => @user }
            else
                format.html { render :action => "new" }
                format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
            end
        end
    end

    # PUT /users/1
    # PUT /users/1.xml
    def update
        @user = User.find(params[:id])

        respond_to do |format|
            if @user.update_attributes(params[:user])
                format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
            end
        end
    end

    # DELETE /users/1
    # DELETE /users/1.xml
    def destroy
        @user = User.find(params[:id])
        @user.destroy

        respond_to do |format|
            format.html { redirect_to(users_url) }
            format.xml  { head :ok }
        end
    end
end

用户模型

class User < ActiveRecord::Base
  acts_as_authentic

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  validates_attachment_presence :avatar
  validates_attachment_size :avatar, :less_than => 5.megabytes
  validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png']
end

一旦包含验证,我在尝试创建或编辑用户时会收到错误

必须设置头像文件名。

I'm trying to use paperclip for the first time. I tried using avatar for user but when I view profile only thing that shows up is the word 'missing'.

class UsersController < ApplicationController
    # GET /users
    # GET /users.xml
    def index
        @users = User.all

        respond_to do |format|
            format.html # index.html.erb
            format.xml  { render :xml => @users }
        end
    end

    # GET /users/1
    # GET /users/1.xml
    def show
        @user = User.find(params[:id])

        respond_to do |format|
            format.html # show.html.erb
            format.xml  { render :xml => @user }
        end
    end

    # GET /users/new
    # GET /users/new.xml
    def new
        @user = User.new

        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @user }
        end
    end

    # GET /users/1/edit
    def edit
        @user = User.find(params[:id])
    end

    # POST /users
    # POST /users.xml
    def create
        @user = User.new(params[:user])

        respond_to do |format|
            if @user.save
                format.html { redirect_to(:users, :notice => 'Registration successfull.') }
                format.xml { render :xml => @user, :status => :created, :location => @user }
            else
                format.html { render :action => "new" }
                format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
            end
        end
    end

    # PUT /users/1
    # PUT /users/1.xml
    def update
        @user = User.find(params[:id])

        respond_to do |format|
            if @user.update_attributes(params[:user])
                format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
            end
        end
    end

    # DELETE /users/1
    # DELETE /users/1.xml
    def destroy
        @user = User.find(params[:id])
        @user.destroy

        respond_to do |format|
            format.html { redirect_to(users_url) }
            format.xml  { head :ok }
        end
    end
end

Users Model

class User < ActiveRecord::Base
  acts_as_authentic

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  validates_attachment_presence :avatar
  validates_attachment_size :avatar, :less_than => 5.megabytes
  validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png']
end

Once I included validations I get error when trying to create or edit a user

Avatar file name must be set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2024-11-12 09:44:32

在您看来的表单定义中,是否有 :multipart =>真的?大致如下:

<%= form_tag({:action => :upload}, :multipart => true) do %>

In the form definition in your view, do you have :multipart => true? Something along the line of:

<%= form_tag({:action => :upload}, :multipart => true) do %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文