2012-12-05 23 views
0

我有一個屬於報表模型的區域模型。我已經使用SimpleForm構建了一個表單部分。當我去new_report_area_path(@report),我得到一個新區域表單,工作得很好。輸入詳細信息並點擊提交,它會創建一個區域並將您帶到區域#show。但新區域表單上的按鈕顯示「更新區域」不是「創建區域」。爲什麼?爲什麼我的form_for area#new有一個按鈕,顯示「更新區域」,而不是「創建區域」

的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 

應用程序/模型/ 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' 

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

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

回答

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 
相關問題