私はpost
と入れ子になったフォームを持っています。place.
私は両方のデータエントリをデータベースに提出したいと思います。私はpost_id
と呼ばれる場所テーブルに外部キーを持っています。投稿の外部キーのように提出する際に私はそれらを関連づけておくと、投稿IDを与え、すべてがpost
とplace
と提出されますか?ありがとうございました!!!NoMethodError in PostsController# "入れ子になったフォームの複数テーブルを作成"
ポストコントローラ:
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 %>
エラーレポート:
` はHTML パラメータとして作成PostsControllerの番号によって、2016年5月11日20時14分53秒0900 処理で:: 1のためにPOST "/投稿を" 開始:{ "UTF8"=> "✓"、" # "、" original_filename = "IMG_0645.JPG"、@ content_type = "image/jpeg" => "画像" => "、"オリジナルのファイル名= "IMG_0645.JPG" 、@headers = "Content-Disposition:フォームデータ。名前= \ "投稿[画像] \";名前= \ "IMG_0645.JPG \" \ r \ nContent-Type:image/jpeg \ r \ n ">" places "=> {" address "=>"無名道路、モクロ、ボスニア・ヘルツェゴビナ "、" streetnumber => "未定義"、 "街路" => "無名道路"、 "都市" => "モクロ"、 "国" => "Republika Srpska"、 "country" => "ボスニア・ヘルツェゴビナ"}、 "=>" "}"、 "latitude" => "43.88522775121559"、 "経度" => "18.6328125"、 "コミット" => "投稿を作成"} ユーザ負荷(0.1ms)SELECT "users"。* FROM " ASC LIMIT 1 [["id"、16]] (0.0ms)トランザクションの開始 Command :: file [ユーザ名] [ユーザ名] -b --mime '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1fkkpgg.JPG' コマンド:: identify -format '%wx%h、%[exif:orientation]' '/ var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w。 JPG [0] '2>/dev/null コマンド:: identify -format%m' /var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG[0] ' Command :: convert'/VAR /フォルダ/ TB/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG [0]」-auto-オリエント-resize "640" 'は/ var /フォルダ/ TB/nnp8bs4x34b4ylvjsgq5g0400000gn/T/74ec5b80c34294faf295a1917f07ae9720160511-94657-1rtm7lc' コマンド::ファイル-b --mime '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1rirdao.JPG'
`
ありがとうございました!!あなたのPostController新しい方法で
を追加あなたの質問には、完全なエラーメッセージ – Pavan
IVはそれに取り組んでいくつかのことを変更されて更新してください。私は多くのエラーを取得していないが、私の場所のテーブルisnt場所の入力ボックスの値を入力しています。私は両方のフォームを編集し、エラーメッセージを追加しました!ありがとう! –
許可されていないパラメータ 'places' –