Home>

I'm making an app while watching the Progate rails course. I'm sorry that I wanted to upload my profile picture now. The code comes with Progate as it is, but it doesn't work.
I'm not sure if I look at the error texts that I have never seen before. Only here has stopped for more than half a day.

  • Save images to public/user_images
  • Display saved images on your profile page (images can be changed for each user)

.

Please teach me.

home_controller.rb
def update_user
    @user = User.find_by (id: params [: id])
    @ user.name = params [: name]
    @ user.email = params [: email]
    if params [: image]
      @ user.image_name = "#{@user.id}.jpg"
      image = params [: image]
      File.binwrite ("public/user_images/# {@ user.image_name}", image.read)
    end
    if @ user.save
      flash [: notice] = "User information has been edited"
      redirect_to ("/")
    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
routes.rb
Rails.application.routes.draw do
get "home/test" =>"home # barbajs"

  get ": id/edit_profile" =>"home # edit_profile"
  get "account /: id/edit" =>"home # edit_account"
  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 ": id/setting" =>"home # setting"
  get "pay" =>"pay # payment"
  get "home/new_fc_content" =>"home # new_fc_content"
  post "home/content_create" =>"home # content_create"
end
edit_profile.html.erb (profile edit page)
<h3>Edit profile</h3>
<% = form_tag ("/ user/# {current_user.id}/update", {multipart: true}) do%>
  <p>Username</p>
  <textarea name = "name"><% = current_user.name%></textarea>
  <li>Self introduction change</li>
  <li>Change profile photo</li>
  <p>Image</p>
  <input name = "image" type = "file">
  <input type = "submit" value = "This completes!">
<% end%>
profile.html.erb (profile page)
<div>
<h2><% = @ user.name%></h2>
<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>


  • Answer # 1

    From the error message,"public/user_images/# {@ user.image_name}"becomes"public/user_images /"So
    I feel like@ user.image_name ="# {@ user.id} .jpg"isn't running properly ...

    In the rails console,

    user = User.new
    user.image_name = "foo.jpg"
    p user
    If you execute

    etc., is foo.jpg displayed?