使用轨道中两个模型的字段

时间:2015-10-17 04:04:50

标签: ruby-on-rails rails-models fields-for

发布模型

has_many :post_contents
accepts_nested_attributes_for :post_contents

PostContents模型

belongs_to :post

发布_form.html.erb

<%= form_for(@post) do |f| %>
 <div class="row">
  <div class="col-md-12">
   <div class="form-group">
     <%= f.label :title %>
     <%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
   </div>
   <div class="form-group">
     <%= f.label "Description" %>
     <%= f.text_field :body, class: 'form-control', placeholder: "Enter post description" %>
   </div>
   <div class="form-group">
     <%= fields_for :post_content do |x| %>
       <%= x.label :body %>
       <%= x.cktext_area :body, :ckeditor => {:toolbar => 'Full'} %>
     <% end %>
   </div>
   <div class="form-group">
    <%= f.submit "Save", class: 'btn btn-success' %>
  </div>
<% end %>
 </div>
</div>

我尝试过post_content和post_contents

PostContents引用帖子

class CreatePostContents < ActiveRecord::Migration
  def change
    create_table :post_contents do |t|
      t.references :post, index: true, foreign_key: true
      t.text :body

      t.timestamps null: false
    end
  end
end

和AddCurrentPostContentIdToPosts

class AddCurrentPostContentIdToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :current_post_content_id, :integer
    add_index :posts, :current_post_content_id
  end
end

PostContentController

 def new
  @post_content = PostContent.new
end

def create
  @post_content = PostContent.new(post_content_params)

  respond_to do |format|
   if @post_content.save
    format.html { redirect_to @post_content, notice: 'Post content was successfully created.' }
    format.json { render :show, status: :created, location: @post_content }
  else
    format.html { render :new }
    format.json { render json: @post_content.errors, status: :unprocessable_entity }
   end
 end

 def post_content_params
   params.require(:post_content).permit(:post_id, :body, :posts)
 end
end

PostController中

def new
 @post = Post.new
 authorize @post
end

def create
 @post = Post.new(post_params)
 @post.user = current_user
 respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render :show, status: :created, location: @post }
  else
    format.html { render :new }
    format.json { render json: @post.errors, status: :unprocessable_entity }
   end
 end

 def post_params
   params.require(:post).permit(:title, :body, :current_post_content_id, post_contents_attributes: [:body])
 end
end

抱歉重复的字段(:正文)。

在创建新帖子时,您建议将PostContent:body字段添加到'posts / _form'中?

3 个答案:

答案 0 :(得分:0)

正如何塞所说,这可能是阻碍你输入的强大参数。你可能想这样做:

def post_params
  params.require(:post).permit(:body,:title, post_content_attributes: [:body])
end

答案 1 :(得分:0)

我刚注意到你的参数不对:

"post"=>{"title"=>"Testing params hash", "body"=>"Testing params hash body"}, "post_contents"=>{"body"=>"<p>Testing params hash post_content</p>\r\n"}, "commit"=>"Save"} 

post_ 内容应该在帖子哈希中,并且它在外面。见http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

原因是您的fields_for未与原始帖子对象相关联,请尝试使用此

<%= f.nested_fields_for :post_content do |x| %>

而不是

<%= fields_for :post_content do |x| %>

答案 2 :(得分:0)

你应该改变

<%= fields_for :post_content do |x| %>

<%= f.fields_for :post_contents do |x| %> # wrap the fields_for with f

post_contents哈希不在post哈希中的原因是因为您没有使用fields_for包裹form object f并且还注意到更改{ {1}}至post_content,因为您有post_contents关系。