のServiceProviderが見つかりませんでした。オブジェクトの作成に問題があります。 New-function istは動作しますが、Createにオブジェクトを渡すと、次のエラーがスローされます。 ServiceProviderが存在する場合にServiceを追加したいとします。すべてのユーザーは、自分のために1つのServiceProviderを作成できます。 ServiceProviderにはいくつかのサービスがあります。 私はまた、MIgration-fileのサービステーブルの外部キーを "add_foreign_key:services、:service_provider"で設定しました なぜservice_provider_idが転送されないのかわかりません。ActiveRecord :: RecordNotFound 'id' =
エラー出力
ActiveRecord::RecordNotFound in ServicesController#create
Couldn't find ServiceProvider with 'id'=
Extracted source (around line #55):
private
def current_service_provider
Line 55: @current_service_provider = ServiceProvider.find(params[:id])
end
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"Z4s/ZzbbMmcXA1jAyFnEZZ/8RhS2ZO/UdI6xN5QkKQwXmJJOOo/PVsSdOUcuJvVpFJdaUi/rMtNJq5sAEdz89g==",
"service"=>{"name"=>"dsfg", "address"=>"asdf", "radius"=>"64655", "price"=>"45", "descr"=>"sdfagasrfsdgh"},
"commit"=>"Speichern"}
モデル
class ServiceProvider < ApplicationRecord
belongs_to :user
has_many :services
validates :name, presence: true
validates :street, presence: true
validates :plz, presence: true
validates :location, presence: true
validates :user_id, presence: true
end
class Service < ApplicationRecord
belongs_to :service_provider
has_many :service_photos
validates :name, presence: true, length: {maximum: 50}
validates :address, presence: true
validates :price, presence: true
end
ServicesController
class ServicesController < ApplicationController
before_action :set_service, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show]
def index
@service = current_user.services
end
def show
@service_photos = @service.service_photos
end
def new
@service = Service.new
end
def create
@service = current_service_provider.services.build(service_params)
if @service.save
if params[:images]
params[:images].each do |image|
service.service_photos.create(image: image)
end
end
@service_photos = @service.service_photos
redirect_to edit_service_path(@service), notice: "Gespeichert"
else
render :new
end
end
def edit
end
def update
end
private
def current_service_provider
@current_service_provider = ServiceProvider.find(params[:id])
end
def set_service
@service = Service.find(params[:id])
end
def service_params
params.require(:service).permit(:name, :address, :radius, :price, :descr)
end
end
フォーム
<div class="panel panel-default">
<div class="panel-heading">
Erstelle deinen Service
</div>
<div class="panel-body">
<div class="container">
<%= form_for @service, html: {multipart: true} do |f| %>
<div class="row">
<div class="form-group">
<label>Service Name</label>
<%= f.text_field :name, placeholder: "Servicename", class: "form-control" %>
</div>
</div>
<div class="row">
<div class="form-group">
<label>Adresse</label>
<%= f.text_field :address, placeholder: "Adresse", class: "form-control" %>
</div>
</div>
<div class="row">
<div class="form-group">
<label>Radius</label>
<%= f.text_field :radius, placeholder: "Radius", class: "form-control" %>
</div>
</div>
<div class="row">
<div class="form-group">
<label>Preis</label>
<%= f.text_field :price, placeholder: "Preis", class: "form-control" %>
</div>
</div>
<div class="row">
<div class="form-group">
<label>Beschreibung</label>
<%= f.text_area :descr, rows: 5, placeholder: "Beschreibung", class: "form-control" %>
</div>
</div>
<div class="rows">
<div class="col-md-4">
<div-form-group>
<span class="btn btn-default btn-file">
<i class="fa fa-cloud-upload fa-lg"></i>Fotos hochladen
<%= file_field_tag "images[]", type: :file, multiple: true %>
</span>
</div-form-group>
</div>
</div>
<div id="photos"><%= render 'service_photos/list' %></div>
<div class="actions">
<%= f.submit "Speichern", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
あなたのフォームはどのように見えますか? –
フォームを追加しました – MaxMaster