Ruby on rails 嵌套资源。ActiveRecord::RecordNotFound在Create方法中
这是我的route.rb文件:Ruby on rails 嵌套资源。ActiveRecord::RecordNotFound在Create方法中,ruby-on-rails,Ruby On Rails,这是我的route.rb文件: Rails.application.routes.draw do resources :invoices devise_for :users root to: 'pages#home' resources :users do resources :properties do resources :invoices end end end 虽然在创建新属性时,我对属性控制器中的Create方法没有问题: class Prope
Rails.application.routes.draw do
resources :invoices
devise_for :users
root to: 'pages#home'
resources :users do
resources :properties do
resources :invoices
end
end
end
虽然在创建新属性时,我对属性控制器中的Create方法没有问题:
class PropertiesController < ApplicationController
before_action :authenticate_user!
def index
@properties = Property.all
end
def new
@user = current_user
@property = @user.properties.build
end
def create
@user = current_user
@property = @user.properties.create(property_params)
@property.user_id = current_user.id
if @property.save
flash[:success] = "Nouveau Bien créé!"
redirect_to user_properties_path
else
render 'static_pages/home'
end
end
类属性控制器
这与从属性控制器创建发票时完全不同:
class InvoicesController < ApplicationController
def index
@invoices = Invoice.all.order('created_at DESC')
end
def new
@property = Property.find(params[:property_id])
@invoice = @property.invoices.build
end
def create
@property = Property.find(params[:property_id])
@invoice = @property.invoices.create(invoice_params)
if @invoice.save
redirect_to @invoices
else
render 'new'
end
end
private
def invoice_params
params.require(:invoice).permit(:content)
end
end
class InvoicesController
以下是表单视图:
<%= simple_form_for(@invoice) do |f| %>
<div class="form-inputs">
<%= f.input :content %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Rails服务器一直在喷涌:
“ActiveRecord::RecordNotFound in InvoicesController#create”
找不到没有ID的属性
为什么它可以在新方法中拾取(params[:property_id]),而不能在Create方法中拾取???我猜在您需要传递给用户的表单中,属性,因为
发票
具有嵌套路由
这将是
您可以发布发票
表单吗?我想在表单中您需要传递用户
,属性
,因为发票有嵌套的路由。完成。即使我编辑表单,它仍然会导致问题。这次新视图返回错误消息:ActiveModel::MissingAttributeError invoices#new Showing/Users/rodolphegent/code/mesloyers/app/views/invoices/_form.html.erb,其中第2行出现:无法写入未知属性builder
提取的源代码(第2行附近):1 2 3 4 5 6您的语法错误。这将是
嘿,Emu,是的,它正在工作,谢谢!您需要发布一个答案,以便我可以验证它,因此您可以对此进行评分。再次作为答案发布谢谢。:)