Ruby on rails 两个模型未嵌套但链接的Simpleform未能提交

Ruby on rails 两个模型未嵌套但链接的Simpleform未能提交,ruby-on-rails,ruby,activerecord,rails-activerecord,Ruby On Rails,Ruby,Activerecord,Rails Activerecord,目前,我的应用程序中有两个模型 class Budget < ApplicationRecord belongs_to :project end class Project < ApplicationRecord has_one :budget end 我真的不明白为什么我没有在现有的类上工作 这是我的卷轴 def create respond_to do |format| @budget = Budget.new(params.require("budget"

目前,我的应用程序中有两个模型

class Budget < ApplicationRecord
    belongs_to :project
end
class Project < ApplicationRecord
    has_one :budget
end
我真的不明白为什么我没有在现有的类上工作

这是我的卷轴

def create
respond_to do |format|
  @budget = Budget.new(params.require("budget").permit(:amount))
  if @budget.save # If budget is saved
    puts "Budget successfuly created"
    @project.budget = @budget
    if @project.save
      puts "project successfuly created"
    else
      puts "Unable to create project"
      format.html { render :new, notice: 'Project went wrong' }
    end
    format.html { redirect_to projects_path, notice: 'Success' }
  else # If budget isn't saved
    puts "Unable to create budget"
    format.json { render json: @budget.errors, status: :unprocessable_entity }
    format.html { render :new, notice: 'Budget went wrong' }
  end
end
我正在寻找有关simpleform和rails编码的良好实践,但我无法真正找到与我的案例相关的内容,或者至少我无法识别与我的案例相关的文档

PS:在我的rails控制台中,我得到了以下输出:
无法创建预算

您的控制器有点不正常。我会调查违约情况

想知道如何清理它。这意味着有更好的调试机会

嵌套参数 我看到的一件事是,你的param电话出了问题

@预算=预算.newparams.requirebudget.permit:金额

这就像说:找到预算的参数“金额”

但表单生成的参数不同:

您有一个简单的@projects表单和一个简单的@budget字段

因此,您的参数将嵌套如下:

参数[:项目][:预算]

希望能有帮助

顺便问一下:您是否有设置@project的before_操作


或者在控制器的第6行返回nil。

方法模型的名称在哪里?包括的控制器是预算或项目?视图是预算还是项目?你能不能也放一个显示这个视图的新方法?@PeterHøjlundAndersen:没有,我认为它是rails或active_record方法@Pablo:控制器来自项目,因此作为视图,实际上预算只有创建路线,在它的控制器中创建一个只包含Budget的方法。newEDIT:在最后嵌套的模型似乎更合适,所以会有ITEM_a的Budget,ITEM_B的Budget,等等。。。我想我可以为每一个项目制定一条路线,这应该不是问题。所以现在在我看来:我在我的原始帖子中声明的问题已经不存在了,现在我面临这个问题:我想以相同的形式创建预算和项目,以便在同一时间创建,但如果不在项目中,我就不能创建预算,如果没有预算,我就不能创建项目,所以我被困在这里……我没有为项目设置任何操作之前,我想这是一个很好的提示,我会先测试一下。你修好了吗?有点,目前它已经足够满足我的需要了,但没有达到预期效果。我需要读更多的书,博士。我也重新定义了这个问题,最后我使用了嵌套模型
"undefined method `model_name' for nil:NilClass"
def create
respond_to do |format|
  @budget = Budget.new(params.require("budget").permit(:amount))
  if @budget.save # If budget is saved
    puts "Budget successfuly created"
    @project.budget = @budget
    if @project.save
      puts "project successfuly created"
    else
      puts "Unable to create project"
      format.html { render :new, notice: 'Project went wrong' }
    end
    format.html { redirect_to projects_path, notice: 'Success' }
  else # If budget isn't saved
    puts "Unable to create budget"
    format.json { render json: @budget.errors, status: :unprocessable_entity }
    format.html { render :new, notice: 'Budget went wrong' }
  end
end