我遇到了创建对象的问题。 New-function工作正常,但是当我将对象传递给Create时,会抛出以下错误。 我想在ServiceProvider存在时添加服务。每个用户都可以为自己创建一个ServiceProvider。 ServiceProvider可以提供一些服务。 我还在MIgration文件中设置了services-table的外键 " add_foreign_key:services,:service_provider" 我不知道为什么service_provider_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>
答案 0 :(得分:1)
在&#34;创建&#34;您尝试查找不存在的对象的操作。你可以通过“id”找到对象。保存之后。 在你的情况下,当&#34; current_service_provider&#34;时,params [:id]不存在方法正在呼唤。