在表单错误后使用参数渲染操作

时间:2014-06-12 08:09:09

标签: ruby-on-rails ruby-on-rails-4

我有访问的基本表单,例如:http://url.com/rentals/new/dvd/10。 问题是当发生表单错误时我无法将其重定向到具有相同页面的同一页面 url细分并显示表单错误消息。

rentals_controller.rb:

def create
    @rental = Rental.new(rental_params)

    respond_to do |format|
      if @rental.save
        format.html { redirect_to @rental, notice: 'Rental was successfully created.' }
        format.json { render :show, status: :created, location: @rental }
      else           
        format.html { render :new }            
        format.json { render json: @rental.errors, status: :unprocessable_entity }
      end
    end
  end

的routes.rb

get 'rentals/new/dvd/:dvd_id' => 'rentals#new', as: :new_dvd_rental

我创建了以下模型: 的 dvd.rb

class Dvd < ActiveRecord::Base
  has_many :rentals
  has_many :users, through: :rentals

  validates :title, presence: true
  validates :year, inclusion: {in: 1900..Time.now.year.to_i}, :presence => {:message => 'Year must be from 1900 till current year.'}
  validates :length, inclusion: {in: 1..999}, :presence => {:message => 'DVD length must be in minutes in range 1..999.'}
end

rental.rb

class Rental < ActiveRecord::Base
  belongs_to :user
  belongs_to :dvd
  validates :user_id, presence: true
  validates :total_price, presence: true
end

user.rb

class User < ActiveRecord::Base
  has_many :rentals
  has_many :dvds, through: :rentals
end

以及 rental_controller.rb

class RentalsController < ApplicationController
  before_action :set_rental, only: [:show, :edit, :update, :destroy]

  # GET /rentals
  # GET /rentals.json
  def index
    @rentals = Rental.all
  end

  # GET /rentals/1
  # GET /rentals/1.json
  def show
  end

  # GET /rentals/new
  def new
    @rental = Rental.new
    @users = User.all
    @dvd = Dvd.find(params[:dvd_id])
  end

  # GET /rentals/1/edit
  def edit
  end

  # POST /rentals
  # POST /rentals.json
  def create
    @rental = Rental.new(rental_params)

    respond_to do |format|
      if @rental.save
        format.html { redirect_to @rental, notice: 'Rental was successfully created.' }
        format.json { render :show, status: :created, location: @rental }
      else
        format.html { render :new }
        format.json { render json: @rental.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /rentals/1
  # PATCH/PUT /rentals/1.json
  def update
    respond_to do |format|
      if @rental.update(rental_params)
        format.html { redirect_to @rental, notice: 'Rental was successfully updated.' }
        format.json { render :show, status: :ok, location: @rental }
      else
        format.html { render :edit }
        format.json { render json: @rental.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /rentals/1
  # DELETE /rentals/1.json
  def destroy
    @rental.destroy
    respond_to do |format|
      format.html { redirect_to rental_url, notice: 'Rental was successfully deleted.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_rental
      @rental = Rental.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def rental_params
      params.require(:rental).permit(:dvd_id, :user_id, :rent_date, :return_date, :total_price, :returned)
    end
end

我试图像这样修改租借控制器,但仍然不知道如何传递其他细分如newdvd

render :action => "new", :dvd_id => params[:dvd_id]

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

我想如果你画出一条更加宁静的路线

resources :dvds do
  resources :rentals
end

您将获得http://url.com/dvd/10/rentals/new

等路线

这里你总是得到dvd_id

并在rental_controller中创建方法看起来像

def create
  @dvd = Dvd.find(params[:dvd_id])
  @rental = Rental.new(rental_params)

  respond_to do |format|
    if @rental.save
      format.html { redirect_to @rental, notice: 'Rental was successfully created.' }
      format.json { render :show, status: :created, location: @rental }
    else
      format.html { render :new }
      format.json { render json: @rental.errors, status: :unprocessable_entity }
    end
  end
end

答案 1 :(得分:0)

- 等待@Sanket的想法

<强>路线

问题几乎肯定与您的redirect_to方法

有关

问题是你的controller不知道你正在使用nested resource,因此当你重定向到一个对象时,它可能会带你到它能找到的最简单的路线

我会试试这个:

def create
      ...
      else           
        format.html { render your_nested_resource_path(dvd_id: params[:dvd_id], other: params[:params]) }
      ...
end 

这允许您将请求发送到嵌套路由,Rails在没有支持的情况下不会路由到