Rails 4 LocalJumpError用于嵌套的求助表单

时间:2015-06-29 12:47:13

标签: ruby-on-rails ruby-on-rails-4 form-for friendly-id

我在另一个追索权“预算”中有一个称为“交易”的嵌套追索权。

我想要完成的只是让我的用户能够编辑单个“交易”。但是,当我转到/ 1 / transactions / 1 / edit时,我得到一个LocalJumpError,说“没有给定块(yield)”。 可能有一个非常简单的解决方案,但我还没有找到它。

routes.rb

resources :budgets, :path => '/' do
  resources :transactions
end

budget.rb:

class Budget < ActiveRecord::Base
  belongs_to :user
  has_many :transactions

  validates :amount, presence: true
  validates :title, presence: true
  validates :user, presence: true
  validates :amount, numericality: true

  extend FriendlyId
  friendly_id :title, use: :slugged

  def should_generate_new_friendly_id?
    new_record?
  end
end

transaction.rb

class Transaction < ActiveRecord::Base
  belongs_to :user
  belongs_to :budget

  validates :amount, presence: true
  validates :user, presence: true
  validates :budget, presence: true
  validates :date, presence: true
  validates :amount, numericality: true
  validates :is_positive, :inclusion => {:in => [true, false]}
end

transactions_controller.rb

  def edit
    @budget = Budget.friendly.find(params[:budget_id])
    @transaction = @budget.transaction
  end

在视图中 transactions / edit.html.erb

<%= form_for(@transaction) do |f| %>

我错过了什么?

1 个答案:

答案 0 :(得分:1)

命名模型事务与ActiveRecord :: Transactions冲突。您需要重命名模型。

http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html