Home>

I want to display my profile picture, but it doesn't go well.

Images are stored in public/user_images but not in the database.

After many trials and errors, it has not been solved yet. Could anyone please professor?


routes.rb
Rails.application.routes.draw do
  post "user /: id/update" =>"home # update_user"
  get 'pay/payment'
  devise_for: users
  get "home /: id/likes" =>"home # likes"
  post "likes /: post_id/create" =>"likes # create"
  post "likes /: post_id/destroy" =>"likes # destroy"
  root 'home # index'
  get "home/new_fc" =>"home # new_fc"
  post "home/create" =>"home # create"
  get "home/profile /: id" =>"home # profile"
  get ": id/show_fc" =>"home # show_fc"
  get ": id/edit_fc" =>"home # edit_fc"
  post ": id/update_fc" =>"home # update_fc"
  post ": id/fc/destroy" =>"home # destroy"
  get "pay" =>"pay # payment"
end
home_controller.rb
class HomeController<ApplicationController
  def index
    @posts = Post.all.order (created_at:: desc)
    # @post = Post.find_by (id: params [: id])
    # @user = @ post.user
    @users = User.all.order (created_at:: desc)
  end
  def new_fc
  end
  def create
    @post = Post.find_by (id: current_user.id)
    @post = Post.new (content: params [: content],
            user_id: @ current_user.id)
    if @ post.save
      redirect_to ("/")
    else
      render ("new_fc")
    end
  end
  def show_fc
    @post = Post.find_by (id: params [: id])
    @user = @ post.user
    @likes_count = Like.where (post_id: @ post.id) .count
  end
  def edit_fc
    @post = Post.find_by (id: params [: id])
  end
  def update_fc
    @post = Post.find_by (id: params [: id])
    @ post.content = params [: content]
    if @ post.save
      flash [: notice] = "Editing is complete !!"
      redirect_to ("/ # {@ post.id}/show_fc")
    else
      render ("edit_fc")
    end
  end
  def profile
    @user = User.find_by (id: params [: id])
    @post = Post.find_by (id: params [: id])
    @likes = Like.where (user_id: @ user.id)
  end
  def update_user
    @user = User.find_by (id: params [: id])
    @ user.name = params [: name]
    @ user.email = params [: email]
    # Add processing to save image
    if params [: image]
      @ user.image_name = "#{@user.id}.jpg"
      image = params [: image]
      File.binwrite ("public/user_images/# {@ user.image_name}", image.read)
    end
    @ user.save
      flash [: notice] = "User information has been edited"
      redirect_to ("/ home/profile/#{@user.id}")
  end
  def likes
    @user = User.find_by (id: params [: id])
    @post = Post.find_by (id: params [: id])
    @likes = Like.where (user_id: @ user.id)
  end
  def destroy
    @post = Post.find_by (id: params [: id])
    @ post.destroy
    redirect_to ("/")
  end
end
edit.html.erb
<h2>Edit</h2>
<% = form_for (resource, as: resource_name, url: registration_path (resource_name), html: {method:: put}) do | f |%>
  <% = devise_error_messages!%>
  <div><% = f.label: email, "email address"%><br />
    <% = f.email_field: email, autofocus: true, autocomplete: "email"%>
  </div>
  <% if devise_mapping.confirmable?&&resource.pending_reconfirmation?%>
    <div>Currently waiting confirmation for:<% = resource.unconfirmed_email%></div>
  <% end%>
  <div>
    <% = f.label: password, "password"%><i&(;leave blank if you don't want to change it)</i><br />
    <% = f.password_field: password, autocomplete: "off"%>
    <% if @minimum_password_length%>
      <br />
      <em><% = @minimum_password_length%>characters minimum</em>
    <% end%>
  </div>
  <div>
    <% = f.label: password_confirmation, "confirm password"%><br />
    <% = f.password_field: password_confirmation, autocomplete: "off"%>
  </div>
  <div>
    <% = f.label: current_password, "current password"%><i>(we need your current password to confirm your changes)</i><br />
    <% = f.password_field: current_password, autocomplete: "off"%>
  </div>

  <div>
    <% = f.submit "Save"%>
  </div>
<% end%>
<% = form_tag ("/ user/#{@user.id}/update", {multipart: true}) do%>
          <p>Image</p>
          <input name = "image" type = "file">
          <input type = "submit" value = "Save">
        <% end%>
<h3>Delete account</h3>
<p>Unhappy?<% = button_to "Delete", registration_path (resource_name), data: {confirm: "Are you sure?"}, method:: delete%></p>
<% = link_to "back",: back%>
profile.html.erb
<h3>Profile</h3>
<h2><% = @ user.name%>(profile image)</h2>
<p><% = @ user.name%></p>
<img src = "<% =" /user_images/#{@user.image_name} "%>">
<table>
  <tr>
    <th>Fan Club Total Members</th>
    <th>Number of Fan Clubs Joined</th>
  </tr>
  <tr>
    <td>11</td>
    <td>20</td>
  </tr>
</table>

<% if @ user.id == current_user.id%>
  <p><% = Fan club created by @ user.name%></p>
  <% @ user.posts.each do | post |%>
    <section>
      <div>
        <img src = "http://webcreatorbox.com/sample/images/bear.jpg" alt = "">
        <li><% = link_to (post.content, "/#{post.id}/show_fc")%></li>
      </div>
      <div>
        <p>Site that introduces web design, website creation, and latest web industry information.</p>
      </div>
    </section>
<% end%>

<p><% = Fanclub liked by @ user.name%></p>
<% @ likes.each do | like |%>
  <% post = Post.find_by (id: like.post_id)%>
  <% user = User.find_by (id: like.user_id)%>
  <section>
    <div>
      <li><% = link_to (post.content, "/#{post.id}/show_fc")%></li>
    </div>
    <div>
      <h1><% = link_to (post.user.name, "/home/profile/#{post.user.id}")% ​​></h1>
      <p>Site that introduces web design, website creation, and latest web industry information.</p>
    </div>
  </section>
<% end%>

<ul>
  <li>
    <% = link_to 'change password', edit_user_registration_path%>
  </li>
  <li>
    <% = link_to 'Create a fan club', home_new_fc_path%>
  </li>
</ul>
<% else%>
<p>Yahui</p>
<% end%>
Date_add_image_name_to_users.rb
class AddImageNameToUsers<ActiveRecord :: Migration [5.2]
  def change
    add_column: users,: image_name,: string
  end
end
  • Answer # 1

      

    Images are stored in public/user_images but not in the database.

    If this is the case, isn't this neighborhood wrong?
    Is the image_name column updated in the console screen SQL when @ user.save is done?

    def update_user
        @user = User.find_by (id: params [: id])
        @ user.name = params [: name]
        @ user.email = params [: email]
        # Add processing to save image
        if params [: image]
          @ user.image_name = "#{@user.id}.jpg"
          image = params [: image]
          File.binwrite ("public/user_images/# {@ user.image_name}", image.read)
        end
        @ user.save
          flash [: notice] = "User information has been edited"
          redirect_to ("/ home/profile/#{@user.id}")
      end

    Is@ user.image_namedisplayed in the part where debugging is displayed?

    profile.html.erb

    <img src = "<% =" /user_images/#{@user.image_name} "%>">
    ↓
    <img src = "<% =" /user_images/#{@user.id}.jpg "%>">

    If the image is not displayed,@ user.image_nameis nil?