为什么我的form_for区域#new有一个按钮,上面写着“更新区域”,而不是“创建区域”



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

config/routes.rb:

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

db/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

app/models/area.rb:

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

app/models/report.rb:

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

app/controllers/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

app/views/areas/news.html.haml:

%h1 New Area
= render 'form'

app/views/areas/_form.html.haml:

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

与其创建一个区域,不如构建它,因为它是一个新操作:

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

最新更新