简体   繁体   English

回形针宝石渲染头像上传到不同的视图

[英]Paperclip gem render avatar upload in different view

Hi I'm using the paperclip gem and currently a user of my website can upload his/her avatar in the edit profile page under my registrations ( devise ) the code for uploading goes like this: 嗨,我正在使用回形针gem ,当前我网站的用户可以在我的注册( devise )下的编辑个人资料页面中上传他/她的头像,上传代码如下:

  <div class="field">
    <%= f.label :avatar %><br />
    <%= f.file_field :avatar %>
  </div>

Is there a way that I can render this avatar upload in a different view? 有没有一种方法可以使我的头像上传到其他视图? How do I go about it? 我该怎么办? I want to place it not in my devise view but my view for the user's profile. 我不想将其放置在我的设计视图中,而是将其放置在用户个人资料的视图中。

On your users profile page, you can place a form with update action for your current user. 在用户个人资料页面上,您可以为当前用户放置一个带有更新操作的表单。 It could look something like so: 它可能看起来像这样:

users/registrations/show.html.erb 用户/注册/ show.html.erb

<% if current_user == @user %>
  <%= form_for current_user, url: { :action => :update } do |f| %>
    <!-- ... -->
    <div class="field">
      <%= f.label :avatar %><br />
      <%= f.file_field :avatar %>
    </div>
    <!-- ... -->
    <div class="actions">
      <%= f.submit %>
    </div>        
  <% end %>
<% end %>

If you want to upload the avatar in a different view, you can certainly do it. 如果要在其他视图中上传头像,则可以这样做。

If you want to do it in your devise view, you'll have to edit your database, model and controller. 如果要在设计视图中执行此操作,则必须编辑数据库,模型和控制器。


You can set a "profile" action no problem, we do it here (it's only a test app, you could register for free and try it yourself...): 您可以毫无问题地设置“个人资料”操作,我们在这里进行操作 (这只是一个测试应用,您可以免费注册并自己尝试...):

在此处输入图片说明

You must remember that the User model is not tied to Devise - you can do anything you want with it, regardless of whether it's used for authentication or not. 您必须记住, User模型与Devise无关-您可以使用它进行任何操作,而不管它是否用于身份验证。

You can do the following: 您可以执行以下操作:

#config/routes.rb
resources :profile, controller: :users, only: [:index, :update]

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def update
      current_user.update avatar_params
   end

   private

   def avatar_params
      params.require(:user).permit(:avatar)
   end
end

#app/views/users/index.html.erb
<%= form_for current_user, multipart: true do |f| %>
    <%= f.label :avatar %><br />
    <%= f.file_field :avatar %>
    <%= f.submit %>
<% end %>

This will have to be matched with the appropriate model code etc, but should work to allow you to upload the appropriate avatar. 这必须与适当的模型代码等匹配,但是应该可以使您上载适当的头像。

You can also make the avatar appear in other parts of the app, like this: 您还可以将头像显示在应用程序的其他部分,如下所示:

在此处输入图片说明

This can be done using the standard Paperclip image file path helpers : 这可以使用标准的Paperclip 图像文件路径帮助器完成

<%= image_tag @user.avatar.url %>

If you wanted to tie the avatar to the registration process, you'll have to edit the devise controller to allow the parameters : 如果您要将头像与注册过程联系在一起,则必须编辑devise控制器以允许参数

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:user) << :avatar
  end
end

If you have the correct setup in your model, this should allow you to include the avatar upload facility in your Devise registrations view. 如果您的模型中设置正确,则应该允许您将化身上载功能包括在Devise注册视图中。

In you view you can show it like this 在您看来,可以这样显示

<%= image_tag(@user.image.url %>

or if you have different sizes, then you may render like this 或者如果您使用不同的尺寸,则可以这样渲染

<%= image_tag(@user.image.url(:original) %> # :original is image defined in model.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM