Tomasz
Joined
Activity
forum_threads_controller
class ForumThreadsController <ApplicationController
before_action :authenticate_user!, except: [:index,:show]
before_action :set_forum_thread,except: [:index,:new,:create]
def index
@forum_threads = ForumThread.all
end
def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end
def create
@forum_thread = current_user.forum_threads.new forum_thread_params
if @forum_thread.save
redirect_to @forum_thread
else
render action: :new
end
end
def show
end
private
def set_forum_thread
@forum_thread = ForumThread.find(params[:id])
end
def forum_thread_params
params.require(:forum_thread).permit(:subject,forum_posts_attributes:[:body])
end
end
forum_posts_controller
class ForumPostController < ApplicationController
def index
@forum_posts = @ForumPost.all
end
def new
@forum_post = @ForumPost.new
end
def edit
end
def show
end
def create
end
end
form partial
<%= form_with model: @forum_thread do |form| %>
<%= form.text_field :subject, palceholder: "subject", class: "form-control" %>
<%= form.fields_for :forum_posts do |p| %>
<%= p.text_area :body ,palceholder: " Add a coment", rows: 5, class: " form-control" %>
<% end %>
<% end %>
forum_thread> new.html.erb
<%= render 'forum_threads/form' %>
Shows
unknown attribute 'forum_thread_id' for ForumPost.