我有一个属于报告模型的区域模型。我使用SimpleForm构建了一个部分表单。当我转到new_report_area_path(@report)时,我得到一个工作正常的新区域表单。输入详细信息并点击提交,它会创建一个区域并带您到区域#show。但是新区域表单上的按钮显示“更新区域”而不是“创建区域”。为什么呢?
配置/ routes.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
答案 0 :(得分: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