0

我有一个属于报表模型的区域模型。我已经使用 SimpleForm 构建了一个部分表单。当我转到 new_report_area_path(@report) 时,我得到一个工作正常的新区域表单。输入详细信息并点击提交,它会创建一个区域并将您带到 area#show。但是新区域表单上的按钮显示的是“更新区域”而不是“创建区域”。为什么?

配置/路由.rb:

Testivate::Application.routes.draw do
  resources :reports do
    resources :areas
  end
end

分贝/schema.rb:

ActiveRecord::Schema.define(:version => 20121205045544) do
  create_table "areas", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "report_id"
  end
  create_table "reports", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end
end

应用程序/模型/area.rb:

class Area < ActiveRecord::Base
  attr_accessible :name
  has_many :heuristics
  belongs_to :report
end

应用程序/模型/report.rb:

class Report < ActiveRecord::Base
  attr_accessible :name
  has_many :heuristics
  has_many :areas
end

应用程序/控制器/areas_controller.rb:

class AreasController < ApplicationController  
  filter_resource_access
  def new
    @report = Report.find(params[:report_id])
    @area = @report.areas.create
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  def create
    @report = Report.find(params[:report_id])
    @area = @report.areas.create(params[:area])
    respond_to do |format|
      if @area.save
        format.html { redirect_to report_area_path(@report, @area), notice: 'Area was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end
end

应用程序/视图/区域/news.html.haml:

%h1 New Area
= render 'form'

应用程序/视图/区域/_form.html.haml:

= simple_form_for [@report, @area] do |f|
  = f.error_notification
  = f.input :name
  = f.button :submit
4

1 回答 1

1

而不是创建一个区域,您应该构建它,因为它是一个新操作:

def new
  @report = Report.find(params[:report_id])
  @area = @report.areas.build # build instead of create
  respond_to do |format|
    format.html # new.html.erb
  end
end
于 2012-12-05T07:08:26.957 回答