0
내 레일 앱에 중첩 된 양식이 있습니다. 중첩 된 필드는 데이터베이스에 값을 추가하지 않습니다.어떻게 중첩 된 양식 값을 nil 대신 데이터베이스 테이블에 입력 할 수 있습니까?
주소지를 내 장소 테이블에 추가하고 싶습니다. 주소 필드는 Post 테이블에 해당하는 양식 내에 중첩됩니다. 난 그냥 GitHub를하는 전체 코드를 밀어
내가 내 게시물 컨트롤러 # 만들기 참고로
def create
@post = Post.new(post_params)
@place = Place.new params[:address]
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
에서 뭔가를하고 있지 않다 생각, 내 게시물 형성 :
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :status %><br>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.label :upload %><br>
<%= f.text_field :upload %>
</div>
<%= f.fields_for @post.place do |p| %>
<div class="field">
<%= p.label :address %><br>
<%= p.text_field :address %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
내 게시물 모델 ...
class Post < ActiveRecord::Base
belongs_to :place
belongs_to :user
has_many :comments
has_many :commenters, through: :comments, source: :user
accepts_nested_attributes_for :place
def place_for_form
collection = places.where(place_id: id)
collection.any? ? collection : places.build
end
end
도움이 정말 감사합니다. 나는 이틀 동안이 일에 매달 렸습니다.
Unpermitted parameters: place
변경 후이 @post.place
:place
에
<%= f.fields_for @post.place do |p| %>
<div class="field">
<%= p.label :address %><br>
<%= p.text_field :address %>
</div>
<% end %>
에서, 모든 것이 잘 작동 :