Ruby on Rails Tutorialに追加機能を装備したいです
具体的には、ユーザーのmicropostに対しての返信機能。
これを実装している途中なのですが、アソシエーションについての理解と
form_forの使い方への理解が甘く、うまく動かすことができていないため、
助けをお借りしたいです。。
実装の中で書いたコードを記載します。
Repliesモデルを作成
/app/models/user.rb
ruby
1class User < ApplicationRecord 2 has_many :microposts, dependent: :destroy 3 has_many :replies, through: :micropost_replies, source: :microposts, dependent: :destroy 4 has_many :active_relationships, class_name: "Relationship", 5 foreign_key: "follower_id", 6 dependent: :destroy 7 has_many :passive_relationships, class_name: "Relationship", 8 foreign_key: "followed_id", 9 dependent: :destroy 10 has_many :following, through: :active_relationships, source: :followed 11 has_many :followers, through: :passive_relationships, source: :follower 12 13 .... 14 15end
※2行目has_many: through:が怪しげ...
/app/model/micropost.rb
ruby
1class Micropost < ApplicationRecord 2 belongs_to :user 3 has_many :replies, dependent: :destroy 4 default_scope -> { order(created_at: :desc) } 5 mount_uploader :picture, PictureUploader 6 validates :user_id, presence: true 7 validates :content, presence: true, length: {maximum: 140} 8 validate :picture_size 9 10 .... 11 12end
/app/model/reply.rb
ruby
1class Reply < ApplicationRecord 2 belongs_to :microposts 3 belongs_to :users 4 validates_presence_of :tweet_id, :reply 5end
ルーティングはネストしている
ruby
1Rails.application.routes.draw do 2 get 'password_resets/new' 3 get 'password_resets/edit' 4 root 'static_pages#home' 5 get '/help', to: 'static_pages#help' 6 get '/about', to: 'static_pages#about' 7 get '/contact', to: 'static_pages#contact' 8 get '/signup', to: 'users#new' 9 get '/login', to: 'sessions#new' 10 post '/login', to: 'sessions#create' 11 delete '/logout', to: 'sessions#destroy' 12 resources :users do 13 member do 14 get :following, :followers 15 end 16 end 17 resources :account_activations, only: [:edit] 18 resources :password_resets, only: [:new, :create, :edit, :update] 19 resources :microposts, only: [:show, :create, :destroy] 20 resources :microposts do 21 collection do 22 get :search 23 end 24 end 25 resources :microposts do 26 resources :replies 27 end 28 resources :relationships, only: [:create, :destroy] 29end
replyコントローラーでとりあえずはreplyをnewしたい
ruby
1class RepliesController < ApplicationController 2 before_action :logged_in_user 3 4 def new 5 @micropost = Micropost.find(params[:micropost_id]) 6 @reply = Reply.new 7 end 8 9 def create 10 @reply = Reply.create(create_params) 11 redirect_to micropost_reply_path 12 end 13 14 private 15 def create_params 16 params.require(:reply).permit(:response).merge(micropost_id: params[:micropost_id], user_id: current_user.id) 17 end 18end
※今回は一旦関係なさそうだけどストロングパラメーターの設置の仕方も違いそう
エラーが発生しているのはビュー
ruby
1.... 2 <h2>コメントを残す</h2> 3 <%= form_for[@micropost, @reply] do |f| %> 4 <%= f.text_area :response, placeholder: "返信する", class: 'form-control' %> 5 <%= f.submit "リプライ", class:"btn btn-primary" %> 6 <% end %> 7....
form_forにreplyのresponseカラムの内容を入力して、
表示させたいのですが、エラー発生
考えられるエラーの原因
・form_forの使い方が誤っている(f.text_area 以降)
・アソシエーションができていない
・アソシエーションはできているけど理解できていないため使えていない
お分かりになりましたらご教示いただけませんでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/05 08:23