我有一个带有post
和place.
的嵌套表单我想在提交时将两个数据条目都放入数据库中。我在名为post_id
的地方表中有一个外键。我如何将它们与提交相关联,就像帖子的外键会给出帖子ID一样,所有内容都会被提交post
和place
??谢谢!!!
帖子控制器:
class PostsController < ApplicationController
before_action :authenticate_user!, :except => [:show, :index, :new]
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :owned_post, only: [:edit, :update, :destroy]
def index
@post = Post.new
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = current_user.posts.new
@post.places = Place.new
end
def create
@post = current_user.posts.create(post_params)
if @post.save
flash[:success] = "Your post has been created!"
redirect_to root_path
else
flash[:alert] = "Your new post couldn't be created! Please check the form."
render :new
end
end
def find
@place = Place.new
end
def edit
@place = @post.place;
end
def update
if @post.update(post_params)
flash[:success] = "Post updated."
redirect_to root_path
else
flash.now[:alert] = "Update failed. Please check the form."
render :edit
end
end
def destroy
@post.destroy
flash[:success] = "Your Post has been removed."
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:place_id, :image, :caption, :places_attributes => [:id, :post_id, :city, :country, :address, :streetnumber, :street, :state])
end
def place_params
params.require(:place).permit(:country, :city)
end
def set_post
@post = Post.find(params[:id])
end
def owned_post
unless current_user == @post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
end
表单-home.html:
<%= simple_form_for @post, html: { multipart: true } do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.error_notification %>
</div>
</div>
<div class="container-fluid">
<div class="dont">
<h4>Upload an image (this is required):</h4>
<%= f.input :image, label: false, input_html: { onChange: 'loadFile(event)' } %>
</div>
<%= f.simple_fields_for :places do |o| %>
<div class="dont">
<%= o.input :address, label: false, placeholder: "search", class: 'controls',:input_html =>{:id => 'pac-input'} %>
<input id="latitude" name="latitude" value=" @#{latitude} " type="text">
<input id="longitude" name="longitude" value=" @#{longitude} " type="text">
<input id="action" name="action" value"test" type="hidden">
<%= o.input :streetnumber, label: false, class: 'controls',:input_html =>{:id => 'streetnumber'},:as => :hidden %>
<%= o.input :street, label: false, class: 'controls',:input_html =>{:id => 'street'}, :as => :hidden %>
<%= o.input :city, label: false, class: 'controls',:input_html =>{:id => 'city'}, :as => :hidden %>
<%= o.input :state, label: false, class: 'controls',:input_html =>{:id => 'state'}, :as => :hidden %>
<%= o.input :country, label: false, class: 'controls',:input_html =>{:id => 'country'}, :as => :text, :as => :hidden %>
</div>
<% end %>
<div class="dont">
<%= f.input :caption, label: false, placeholder: 'Add your caption' %>
</div>
<div class="dont">
<%= f.button :submit, class: 'btn-success btn-block' %>
</div>
</div>
<% end %>
错误报告:
` 在2016-05-11 20:14:53 +0900开始发布“/ posts”for :: 1 由PostsController处理#create as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“cJP0UGjMFD244w2D4DqLC + VV86wO / i + ScOxbKUowMKSxx9JnwOQ77EFeyLAZY1E3 / YSQnOOZ2nIbu / Dc9mok6w ==”,“post”=&gt; {“image”=&gt;#, @ original_filename =“IMG_0645.JPG”,@ content_type =“image / jpeg”,@ headers =“Content-Disposition:form-data; name = \”post [image] \“; filename = \”IMG_0645.JPG \“ \ r \ nContent-Type:image / jpeg \ r \ n“&gt;,”places“=&gt; {”address“=&gt;”未命名的道路,Mokro,波斯尼亚和黑塞哥维那“,”streetnumber“=&gt;”undefined“ “,”街道“=&gt;”未命名道路“,”城市“=&gt;”Mokro“,”州“=&gt;”斯普斯卡共和国“,”国家“=&gt;”波斯尼亚和黑塞哥维那“},”标题“ =&gt;“”},“latitude”=&gt;“43.88522775121559”,“经度”=&gt;“18.6328125”,“提交”=&gt;“创建帖子”} 用户负载(0.1ms)SELECT“users”。* FROM“users”WHERE“users”。“id”=? ORDER BY“users”。“id”ASC LIMIT 1 [[“id”,16]] 未允许的参数:地点 (0.0ms)开始交易 Command :: file -b --mime'/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1fkkpgg.JPG' Command :: identify -format'%wx%h,%[exif:orientation]''/ var / folder / tb / nnp8bs4x34b4ylvjsgq5g0400000gn / T / 6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG [0]'2&gt; / dev / null Command :: identify -format%m'/ var / folder / tb / nnp8bs4x34b4ylvjsgq5g0400000gn / T / 6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG [0]' 命令::转换 '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG[0]' - 自动定向调整大小 “640”“的/ var /文件夹/ TB / nnp8bs4x34b4ylvjsgq5g0400000gn / T / 74ec5b80c34294faf295a1917f07ae9720160511- 94657-1rtm7lc” Command :: file -b --mime'/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1rirdao.JPG'
`
谢谢!
答案 0 :(得分:1)
在你的PostController新方法中添加
@post = current_user.posts.new
@places = @post.places.build
在你的Post模型中添加
accepts_nested_attributes_for :places