簡體   English   中英

以Rails形式使用來自兩個模型的字段

[英]Using fields from two models in rails form

郵政模型

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

后控制器

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

抱歉,重復字段(:body)。

在創建新帖子時,您建議如何將PostContent:body字段添加到“ posts / _form”中?

正如Jose所說,可能是強大的參數阻止了您的輸入。 您可能要這樣做:

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

我只是注意到您的參數不正確:

"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_ 內容應位於posts哈希值之內,並且位於外部。 參見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| %>

你應該改變

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

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

post_contents哈希不在post哈希之內的原因是因為您沒有使用form object f包裝fields_for ,並且還因為具有has_many關系而注意到post_content更改為post_contents

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM