ruby-on-rails - RoR : update action. 出错时的渲染路径

标签 ruby-on-rails validation render edit

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/

相关文章:

mysql - 无法在数据库中创建记录

javascript - rails : dynamic select on multistep form not keeping selected

spring-boot - 将 @Future 和 LocalDate 的自定义 ConstraintValidator 添加到 Spring Boot 项目

java - 用户的业务规则 Java 应用程序

Python + Mapnik : Example on how to render a map with a gps track on it

javascript - 提交 Rails 表单是否可以避免离开当前 View ?

css - Heroku 找不到带有 stylesheet_tag 命令的 css(在本地工作)

ruby-on-rails - 应用程序错误-heroku 检查日志?

java - Spring 动态验证

javascript - React 中多个组件的渲染优先级