次のように設定しようとしています: ユーザーにはメンバーシップを通じて多くのグループがあり、グループには多くのイベントがあり、イベントには多くの投稿があります。
グループとそのすべてのイベントを表示するという私の見解では、ドロップダウンから正しいグループを選択し、コメントを書いて送信することで、ユーザーが新しい投稿を作成できるようにしたいと考えています。現在、collection_select を使用して投稿を作成していますが、event_id が ActiveRecord に渡されていません。つまり、投稿は作成されていますが、event_id (またはコメントさえも) がありません。
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, through: :memberships
has_many :posts
end
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :events, dependent: :destroy
has_many :users, through: :memberships
end
class Event < ActiveRecord::Base
belongs_to :group
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
class GroupsController < ApplicationController
def show
#define new post
@new_post = Post.new
end
end
class PostsController < ApplicationController
def create
if @post = Post.create(params[post_params])
flash[:success] = "Post Created!"
else
redirect_to group_url
end
end
private
def post_params
params.require(:post).permit(:event_id, :comment)
end
end
<h1>New Post:</h1>
<%=form_for([@new_post]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class = "field">
<%= f.label :event_name %>
<%= f.collection_select(:event_id, Event.all, :id, :title) %>
</div>
<div class = "field">
<%= f.text_area :comment, placeholder: "New Post..." %>
</div>
<%=f.submit "Submit", class: "btn btn-large btn-primary" %>
<%end%>
ルートがネストされているため、group_id が Posts コントローラーに渡されることはなく、設定できないと感じています。しかし、それよりも多くの間違いがあると確信しています...