@seiyarick (seiya)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

サインイン時の名前をマイページで表示したいです

Q&A

Closed

解決したいこと

サインアップした時の名前をマイページで表示できるようにしたいです。

データベースに nameの記述がありサインイン時に名前、メールアドレス、パスワードを入力する欄がありますが、ログインしたユーザーをrails cで調べると以下のようになります。サインイン時にnameが保存できていないためnameがnilとなりビューで表示できていないと考えています。
どの箇所を修正すれば改善されるか教えていただきたいです。
よろしくお願いします。

 => #<User id: 1, email: "k@k", created_at: "2022-04-09 08:09:17", updated_at: "2022-04-09 08:09:17", name: nil, profile: nil, is_deleted: false> 

[ビューの記述]
*/users/show.html.erb

<h1>Users#show</h1>
<p>Find me in app/views/users/show.html.erb</p>
<%= @user.name %>

[コントローラ]
*/users_controller.rb

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

*/application_controller.

 protect_from_forgery with: :exception

  def after_sign_in_path_for(resource)
    user_path(current_user.id)
  end

  def after_sign_out_path_for(resource)
    root_path
  end

  def after_sign_up_path_for(resource)
    user_path(current_user.id)
  end
end

*/registrations/new.html.erb

<h2>Sign up</h2>

<%= form_with model: @user, url: registration_path(resource_name) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <div class="field">
    <%= f.label :名前 %><br />
    <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "new-password" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>

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

*/db/schema.rb

ActiveRecord::Schema.define(version: 2022_04_09_064638) do
create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.text "profile"
    t.boolean "is_deleted", default: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end
0 likes

No Answers yet.

Your answer might help someone💌