新主题表单包含标题,说明和首发内容的字段。
提交后,应创建包含标题,说明,用户ID和论坛ID值的主题,以及包含内容,用户ID和主题ID值的帖子。但是,帖子的内容并没有保存到表中,尽管用户ID和主题ID都是。
视图/主题/ _form.html.erb
<%= form_for(@topic) do |f| %>
<% if @topic.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@topic.errors.count, "error") %> prohibited this topic from being saved:</h2>
<ul>
<% @topic.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% if params[:forum] %><input type="hidden" id="topic_forum_id" name="topic[forum_id]" value="<%= params[:forum] %>" /><% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<textarea name="post[content]" class="form-control" cols="80" rows="20"><%= @post.content %></textarea>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
控制器/ topics_controller.rb
def new
@topic = Topic.new
@post = Post.new
end
def create
user_id = current_user.id || 1 # temporary assignment until guest account generated
@topic = Topic.new(title: params[:topic][:title], description: params[:topic][:description], forum_id: params[:topic][:forum_id], user_id: user_id)
if @topic.save
@post = Post.new(content: params[:post][:content], topic_id: @topic.id, user_id: user_id)
if @post.save
flash[:notice] = "Successfully created topic."
redirect_to "/topics/#{@topic.id}"
else
render action: 'new'
end
else
render action: 'new
end
end
模型/ post.rb
class Post < ActiveRecord::Base
belongs_to :topic
belongs_to :user
has_many :replies, :dependent => :nullify
validates :content, presence: true
attr_accessor :content
end
模型/ topic.rb
class Topic < ActiveRecord::Base
belongs_to :forum
belongs_to :user
has_many :posts, :dependent => :destroy
validates :title, presence: true, length: { maximum: 255 }
validates :user_id, presence: true
validates :forum_id, presence: true
end
答案 0 :(得分:0)
我会像下面这样实现accepsts_nested_attributes
的概念来处理你的情况。
#topic.rb
class Topic < ActiveRecord::Base
belongs_to :forum
belongs_to :user
has_many :posts, :dependent => :destroy
accepts_nested_attributes_for :posts
validates :title, presence: true, length: { maximum: 255 }
validates :user_id, presence: true
validates :forum_id, presence: true
end
#topics_controller.rb
def new
@topic = Topic.new
@post = @topic.posts.build
end
def create
user_id = current_user.id || 1 # temporary assignment until guest account generated
@topic = Topic.new(params[:topic])
if @topic.save
@post = Post.new(params[:post])
@post.topic_id = @topic.id
@post.user_id = user_id
if @post.save
flash[:notice] = "Successfully created topic."
redirect_to @topic
else
render action: 'new'
end
else
render action: 'new'
end
end
#form
<%= form_for(@topic) do |f| %>
<% if @topic.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@topic.errors.count, "error") %> prohibited this topic from being saved:</h2>
<ul>
<% @topic.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% if params[:forum] %><input type="hidden" id="topic_forum_id" name="topic[forum_id]" value="<%= params[:forum] %>" /><% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<%= f.fields_for @post do |p| %>
<div class="field">
<%= p.text_area :content, cols: 80, rows: 20, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>