如何在rails中创建两步形式?

时间:2016-10-03 16:09:11

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

我对rails有疑问。我正在创建一个表单供用户注册。我想要做的是,在用户按下“提交”按钮后,我想将用户重定向到另一个页面,该页面显示用户刚刚填写的表单中的所有信息(只读)。

这是我的控制器

class PermitsController < ApplicationController
  before_action :set_permit, only: [:show, :destroy]
  def index
    @permits = Permit.all
  end

  def new
    @permits = Permit.new
  end

  def create

    @permits = current_user.permits.build(permit_params)
      if @permits.save
        redirect_to invoice_path
      else
        render 'new'
      end

      end


  def destroy
    Permit.destroy_all(user_id: current_user)
    respond_to do |format|
      format.html { redirect_to root_path, notice: 'Permit was successfully canceled.' }
      format.json { head :no_content }
    end
  end

def invoice
  @permits = current_user.permits(permit_params)

end

  def show
    @user = User.find(params[:id])
    @permits = @user.permits.paginate(permit_params)
  end

  def update
    @permits = Permit.where(user_id: current_user).take
    respond_to do |format|
      if @permits.update(permit_params)
        format.html { redirect_to root_path}
        flash[:success] = "Permit successfully updated"
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def permit_params
    params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
  end
end

这是用户填写的表格

<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@permits) do |f| %>

        <%= f.label :"Vehicle" %>
        <%= f.text_field :vehicle_type, class: 'form-control' %>

        <%= f.label :"License Plate" %>
        <%= f.text_field :carplate, class: 'form-control' %>

        <%= f.label :"Student ID" %>
        <%= f.text_field :studentid, class: 'form-control' %>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control' %>

        <%= f.label :"Department of applicant" %>
        <%= f.text_field :department, class: 'form-control' %>

        <%= f.label :permit_start %>
        <%= f.date_select :permitstart, class: 'form-control' %>

        <%= f.label :permit_end %>
        <%= f.date_select :permitend,  class: 'form-control'  %>


        <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以在newcreate之间添加其他操作。

# config/routes.rb
resources :permit do
  collection do
    post :confirm
  end
end

即使路线没有创建资源,我们使用POST的原因是我们不想在请求网址中传递任何用户信息。

class PermitsController < ApplicationController
  # POST /permits/confirm
  def confirm
    @fields = %i[vehicle_type, carplate, studentid, name, department, permitstart, permitend]
    @permit = current_user.permits.build(permit_params)
    render :new and return unless @permit.valid? 
  end
end
如果输入首先无效,则

render :new and return unless @permit.valid?会快速删除该过程并再次呈现:new表单。

由于我们正在使用POST,因此new.html.erbconfirm.html.erb都需要一个表单,所有这些输入都不会很好,所以我们将它们提取到部分:

<% # /views/permits/_inputs.html.erb %>
<% 
  input_options ||= {}
  input_options[:class] ||= 'form-control'
%>

<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, input_options%>

<%= f.label :"License Plate" %>
<%= f.text_field :carplate, input_options %>

<%= f.label :"Student ID" %>
<%= f.text_field :studentid, input_options %>

<%= f.label :name %>
<%= f.text_field :name, input_options %>

<%= f.label :"Department of applicant" %>
<%= f.text_field :department, input_options %>

<%= f.label :permit_start %>
<%= f.date_select :permitstart, input_options %>

<%= f.label :permit_end %>
<%= f.date_select :permitend,  input_options  %>

因此,请指出new.html.erb表单,以便提交/permits/confirm

<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@permits, url: '/permits/confirm_permits_path') do |f| %>
       <% render partial: :inputs %>
       <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

并创建一个/views/permits/confirm.html.erb视图:

<% provide(:title, 'Confirm Permit application') %>
<h1>Confirm Permit application</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@permits) do |f| %>
       <% render partial: :inputs, input_options: { readonly: 'readonly' } %>
    <% end %>
  </div>
</div>