Rails 项目:Project
有很多Ticket
的。
修改工单的路径:/projects/12/tickets/11/edit
当更新票证和验证失败时,我使用 render :action => "edit"
.
但是这次编辑 View 渲染的时候,路径变成了/tickets/11/
这意味着我丢失了一些参数。我怎样才能保持原来的路径?
路线.rb:
resources :projects do
resources :tickets
end
resources :tickets
ticket_controller.rb
def new
@ticket = Ticket.new
end
def create
@ticket = Ticket.new(params[:ticket])
@ticket.user_id = session[:user_id]
respond_to do |format|
if @ticket.save
format.html { redirect_to project_path(@ticket.project), :notice => "Ticket was created." }
else
format.html { render :action => "new" }
end
end
end
def edit
@ticket = Ticket.find(params[:id])
end
def update
@ticket = Ticket.find(params[:id])
respond_to do |format|
if @ticket.update_attributes(params[:ticket])
format.html { redirect_to project_ticket_path(@ticket.project, @ticket), :notice => "Ticket was updated." }
else
format.html { render :action => "edit" }
end
end
end
最佳答案
看看http://guides.rubyonrails.org/routing.html#nested-resources .
您应该能够使用嵌套路由助手从 Controller 重定向到嵌套资源,例如 project_ticket_path(@project, @ticket)
.
关于ruby-on-rails - RoR : update action. 出错时的渲染路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811621/