简体   繁体   中英

Using fields from two models in rails form

Post Model

has_many :post_contents
accepts_nested_attributes_for :post_contents

PostContents Model

belongs_to :post

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>

I've tried post_content and post_contents

PostContents references post

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

and 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

Sorry for the duplicate fields (:body).

What do you recommend to add the PostContent :body field to the 'posts/_form' when creating a new Post?

As Jose said, it's probably strong parameters blocking your input. You may want to do this:

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

I just noticed your params are not right:

"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_ contents should be within the posts hash and it is outside. See http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

The reason is that your fields_for is not associated to the original post object, try using this

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

instead of

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

You should change

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

to

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

The reason why the post_contents hash is not inside the post hash is because you didn't wrapped fields_for with the form object f and also notice the change post_content to post_contents since you have has_many relation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM