まず、アカウントが存在しており、has_oneでアカウントプロフィールがあり、プロフィールを自由に作成して編集をできるようにしたいと考えています。パラメーターにデータが渡されているのをサーバーログから確認できますが、保存がされず、どこに問題があるのかよくわからない状態です。よろしくお願いします。
account_content.rb
class AccountContent < ApplicationRecord belongs_to :account validates :account_id, presence: true validates :profile, presence: true, length: { maximum: 300 } end
account.rb
class Account < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable after_create :build_profile devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower has_one :account_contents, dependent: :destroy, foreign_key: "account_id" # ユーザーをフォローする def follow(other_account) following << other_account end # ユーザーをフォロー解除する def unfollow(other_account) active_relationships.find_by(followed_id: other_account.id).destroy end # 現在のユーザーがフォローしてたらtrueを返す def following?(other_account) following.include?(other_account) end end
account_contents_controller.rb
class AccountContentsController < ApplicationController def create @profile = AccountContent.create(profile_params) if @profile.save flash[:notice] = "プロフ登録できました" else flash[:notice] = "登録に失敗しました" end end def index end def edit @profile = AccountContent.find(params[:id]) end def update @profile = AccountContent.find(params[:id]) @profile.update(profile_params) end def new @profile=AccountContent.new end def show @profile = AccountContent.find(profile_params) end private def find_profile @profile = AccountContent.find(params[:id]) end def profile_params params.permit(:account_contents).permit(:profile,:account_id) end end
new.html.erb
this is account_contents new <div class-="container-fluid"> <div class-="row justify-content-center"> <div class-="col-md-9"> <%= form_for(@profile) do |f| %> <div class="field"> <%= f.text_area :profile, placeholder: "Compose new micropost..." %> <%= f.hidden_field :account_id, value: current_account.id %> </div> <%= f.submit "Post", class: "btn btn-primary" %> <% end %> </div> </div> </div>
サーバーログ
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TA6zJfufoTw2bqDQFVTaJMAgPK5ijTJR+5lz7RuYexjbLUQ1L2sOhtfhpCl70SjG2peRnUvRCu5g5M47TwBk7Q==", "account_content"=>{"profile"=>"YESSSS", "account_id"=>"1"}, "commit"=>"Post"} Unpermitted parameters: :utf8, :authenticity_token, :account_content, :commit (0.1ms) begin transaction (0.0ms) rollback transaction (0.0ms) begin transaction (0.0ms) rollback transaction Rendering account_contents/create.html.erb within layouts/application Rendered account_contents/create.html.erb within layouts/application (0.3ms) Account Load (0.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = ? ORDER BY "accounts"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Rendered layouts/_header.html.erb (2.9ms) Rendered layouts/_btmnavbar.html.erb (0.8ms) Completed 200 OK in 45ms (Views: 39.4ms | ActiveRecord: 0.4ms)
サーバーログの追加
Started POST "/account_contents" for 111.217.105.86 at 2020-03-15 23:50:24 +0000 Cannot render console from 111.217.105.86! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AccountContentsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"HxsG+Ujsh+G0+TfAfL6KHCljPLMfCQW43VS0u50GEFWIOPHpnBgoW1V2MzkSO3j+M9SRgDZVPQdGKQltyZ4PoA==", "account_content"=>{"profile"=>"weeeeee", "account_id"=>"1"}, "commit"=>"Post"} Unpermitted parameters: :utf8, :authenticity_token, :account_content, :commit (0.1ms) begin transaction (0.1ms) rollback transaction (0.0ms) begin transaction (0.0ms) rollback transaction Rendering account_contents/create.html.erb within layouts/application Rendered account_contents/create.html.erb within layouts/application (0.3ms) Account Load (0.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = ? ORDER BY "accounts"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Rendered layouts/_header.html.erb (3.3ms) Rendered layouts/_btmnavbar.html.erb (0.8ms) Completed 200 OK in 366ms (Views: 38.0ms | ActiveRecord: 0.4ms)
回答1件
あなたの回答
tips
プレビュー