我有两个模型
class Project < ActiveRecord::Base
attr_accessible :created_at, :name, :status, :company_id
has_many :phases, :dependent => :destroy
end
class Phase < ActiveRecord::Base
attr_accessible :created_at, :date_due, :name, :project_id, :status
belongs_to :project
end
创建新阶段后,我想将用户转发到新阶段所属项目的显示项目页面。
我已经尝试过这个,以及一些变体,但不太确定最好的写法:
def create
@phase = Phase.new(params[:phase])
respond_to do |format|
if @phase.save
redirect_to @phase.project
else
format.html { render action: "new" }
format.json { render json: @phase.errors, status: :unprocessable_entity }
end
end
end
谢谢!
编辑 1
耙路线:
phases GET /phases(.:format) phases#index
POST /phases(.:format) phases#create
new_phase GET /phases/new(.:format) phases#new
edit_phase GET /phases/:id/edit(.:format) phases#edit
phase GET /phases/:id(.:format) phases#show
PUT /phases/:id(.:format) phases#update
DELETE /phases/:id(.:format) phases#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
编辑 2
更新的控制器 - 现在工作。需要删除 respond_to 块(也可能只包括重定向周围的不同格式选项)
def create
@phase = Phase.new(params[:phase])
if @phase.save
redirect_to project_url(@phase.project)
else
render action: "new"
end
end