質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

233閲覧

プロフィール作成ページが作れません

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/03/15 11:10

編集2020/03/15 23:56

まず、アカウントが存在しており、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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

no1knows

2020/03/15 12:38

> どこに問題があるのかよくわからない状態です。 Unpermitted parameters: :utf8, :authenticity_token, :account_content, :account_id, :commit
退会済みユーザー

退会済みユーザー

2020/03/15 12:45

has_one, belongs_toの関係を結ぶのが間違っているということなのでしょうか?
guest

回答1

0

ベストアンサー

Parameters: {"utf8"=>"✓", "authenticity_token"=>"", "account_content"=>{"profile"=>"yay"}, "account_id"=>"1", "commit"=>"Post"}

というのに対し
params.permit(:account_contents).permit(:profile,:account_id)
なので :account_id が渡っていません
hidden_field_tag を 
f.hidden_field :account_id, value: current_account.id

追記
params.permit(:account_contents). でなく
params.permit(:account_content).  に

投稿2020/03/15 14:52

編集2020/03/15 21:38
winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/03/15 23:55 編集

試してみたのですけど、保存されずに上のようなエラーが出ました。(更新しました) 追記 書いて下さった追記を実行しましたが、保存はされませんでした。サーバーログも追加しました。
winterboum

2020/03/30 07:34

ここかな params.permit(:account_contents) ↓ params.require(:account_contents)
退会済みユーザー

退会済みユーザー

2020/03/30 07:36

できました!ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問