【问题标题】:How do I display my paperclip image on a profile page?如何在个人资料页面上显示我的回形针图像?
【发布时间】:2015-11-21 19:35:53
【问题描述】:

我对 Rails 很陌生,正在尝试通过构建个人资料页面来进行一些练习。我已经实现了设计,一旦用户登录,他们就可以单击一个链接,该链接显示包含有关他们的信息的个人资料。现在它只显示他们的姓名、出生日期和性别。我希望它也显示它们的图像。所以我捆绑了回形针 gem 并阅读了一些教程来尝试配置它。我已经准备好代码,以便用户可以单击“选择文件”并选择图像。但是,当我单击“更新”时,它会给我一个错误。我希望图像显示在他们个人资料的同一显示页面上。

profiles_controller.rb

  class Users::ProfilesController < ApplicationController
    def edit
      @profile = Profile.create(params[:photo])
    end

    def create
    end

    private 

    def profile_params
      params.require(:profile).permit(:photo)
    end
  end

profiles/edit.html.erb

<h2>Edit Account Profile </h2>


<%= form_for(@profile, url: profiles_edit_path(@profile), html: { :multipart => true }) do |f| %>


  <li>
    <%= f.label :photo, "Photo" %>
    <%= f.file_field :photo %>
  </li>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

<%= link_to "Back", :back %>

registrations/show.html.erb

<%= image_tag @profile.photo.url %>
<br>
<%= @user.email %>
<br>
<%= @user.first_name %>
<%= @user.last_name %>
<br>
<%= @user.date_of_birth %>
<br>
<%= @user.is_female %>

registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  def show 
      @user = User.find(params[:id])
  end


  private

    def sign_up_params
      params.require(:user).permit(:first_name, :last_name, :is_female, :date_of_birth,  :email, :password, :password_confirmation)
    end

    def account_update_params
      params.require(:user).permit(:first_name, :last_name, :is_female, :date_of_birth, :email, :password, :password_confirmation, :current_password)
    end

end

routes.rb

Rails.application.routes.draw do
  devise_for :users, :controllers => { :registrations => 'users/registrations', :profiles => 'users/profiles' }

  devise_scope :user do
    get 'profiles/edit' => 'users/profiles#edit'
    post 'profiles/edit' => 'users/profiles#edit'
    get 'users/:id' => 'users/registrations#show', as: :user 
  end


  root to: "home#index"


end

点击更新后出错:

路由错误 没有路由匹配 [PATCH] "/profiles/edit.21"

感谢您的帮助,如果需要任何进一步的信息,请告诉我。

【问题讨论】:

    标签: ruby-on-rails image devise paperclip image-uploading


    【解决方案1】:

    试试:

    patch 'profiles/:id' => 'users/profiles#update'
    

    表单助手应该是这样的:

    <%= form_for(@profile, html: { :multipart => true }) do |f| %>
    

    希望对你有帮助!!

    【讨论】:

    • 我找不到带有 'id'= 的配置文件我假设我没有创建任何配置文件...我觉得我必须添加一些逻辑来连接用户表与 Profile 表。
    【解决方案2】:

    尝试将此路由添加到 devise_scope 以传递该错误

    patch 'profiles/edit' => 'users/profiles#edit'
    

    顺便说一句,在 ProfilesController 中

    • 应在create 操作中创建新配置文件
    • edit 操作中,您应该加载已存在的配置文件并渲染编辑视图

    更新:

    • edit 视图中的表单将向操作update 提交数据,我们需要再次获取配置文件并为其更新数据

    您的路线不正确,在编辑个人资料时,我们应该提交一个类似users/profiles/4/update的网址

    你可以如下声明路由

     devise_for :users, :controllers => { :registrations => 'users/registrations' }
     namespace :users do
       resources :profiles
     end
    

    update 操作中,您检索该配置文件p = Profile.find(params[:id]) 并更新它p.photo = params[:photo]; p.save 或您想要的任何内容

    【讨论】:

    • 感谢您的帮助!正如你所建议的,我将补丁 'profiles/edit' => 'users/profiles#edit' 添加到路由文件中。我还将新配置文件的创建移到了创建操作中。现在我在 Users::ProfilesController#edit 中收到 ActiveRecord::RecordNotFound
    • 这听起来像是在配置文件控制器的编辑操作中,我必须放置一些逻辑来加载现有用户的个人资料页面......
    • @DamonClark 我刚刚更新了答案,希望对你有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多