ActiveModel多步骤表单未显示下一步

时间:2012-12-20 21:26:40

标签: ruby-on-rails forms routes modal-dialog activemodel

所以我一直在使用Active模型在ruby on rails(1.9.3)上进行多步骤表单,同时遵循railscast#217和#219,并且遇到路由到我的表单中的后续步骤的问题/ awizard / new to / awizard / 1希望有人可以帮助我。 (我不能使用表单gem并且必须自己编写)我认为这是一个路由类型问题(我没有使用我给模型的id,但我无法弄清楚我应该在哪里使用它)一点帮助会非常感谢!

我的控制器代码 - (contollers / awizard_controller.rb)

class AwizardController < ApplicationController

  def new
    # New Asset Wizard
    @wizard = Awizard.new(id: 1)
    # Set session variable as initial step
    session[:wizard_step] = @wizard.current_step
  end

  def update
    @wizard = Awizard.new(id: 1) unless !@wizard.nil?
    @wizard.current_step = session[:wizard_step] unless nil

    if @wizard.valid?
      if params[:back_button]
        @wizard.previous_step
      elsif @wizard.last_step?
        @wizard.save if @wizard.all_valid?
      else
        @wizard.next_step
      end
      session[:wizard_step] = @wizard.current_step
    end

    if @wizard.changed?
      render 'form'
    else
      @wizard.save
    end
  end

  def show
    render 'form'
  end

end

我的模型 - (models / awizard.rb)

class AWizard
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Dirty
include ActiveModel::Naming

#This class is used to manage the wizard steps using ActiveModel (not ActiveRecord)

attr_accessor :id
attr_writer :current_step  #used to write to current step
define_attribute_methods [:current_step] #used for marking change

def initialize(attributes = {})
   attributes.each do |name, value|
     send("#{name}=", value)
   end
end

def current_step
  @current_step || steps.first
end

def steps
  %w[step1 step2 step3] #make list of steps (partials)
end

def next_step
  current_step_will_change! #mark changed when moving stepped
  self.current_step = steps[steps.index(current_step)+1] unless last_step?
end

def previous_step
  current_step_will_change! #mark changed when moving stepped
  self.current_step = steps[steps.index(current_step)-1] unless first_step?
end

def first_step?
  current_step == steps.first
end

def last_step?
  current_step == steps.last
end

def all_valid?
  steps.all? do |step|
    self.current_step = step
    valid?
  end
end

def step(val)
  current_step_will_change!
  self.current_step = steps[val]
end

def persisted?
  self.id == 1
end

def save
 #will do later
end

end

我的观点(/视图/ awizard / _form.html.erb)

<%=  content_for :awizard_form do%>
  <%= form_for(@wizard) do |f| %>
    <%= render "#{@wizard.current_step}_step", :f => f %>
    <%= f.submit "Previous", :name => "back_button" unless @wizard.first_step? %>
    <%= f.submit "Continue", :name => "step" unless @wizard.last_step? %>
  <% end %>
<% end %>

(适用/视图/ awizard / _step1.html.erb)

<div class="field">
  <%= f.label 'Step1' %><br />
</div>

(适用/视图/ awizard / _step2.html.erb)

<div class="field">
  <%= f.label 'Step2' %><br />
</div>

(适用/视图/ awizard / _step3.html.erb)

<div class="field">
  <%= f.label 'Step3' %><br />
</div>

路线代码

resources :awizard

错误消息 点击第一个继续按钮后得到的错误信息是这样的 - “模板缺失

缺少模板awizard /表单,应用程序/表单{:locale =&gt; [:en],:formats =&gt; [:html],:handlers =&gt; [:erb,:builder,:coffee]} 。搜索:*“fakepath / app / views”“

更深入的错误日志如下所示 -

在2013-01-07 10:12:06 +1300开始为127.0.0.1开始PUT“/ awizard / 1” 由AwizardController处理#update为HTML   参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“”,“awizard”=&gt; {“data”=&gt;“data”},“step”=&gt;“继续”, “ID”=&gt; “中1”} 在1毫秒内完成500内部服务器错误

ActionView :: MissingTemplate(缺少模板awizard /表单,应用程序/表单{:locale =&gt; [:en],:formats =&gt; [:html] ,:handlers =&gt; [:erb,:builder ,:coffee]}。搜索:   *“fakepath / app / views” ):   app / controllers / awizard_controller.rb:40:在`update'

在救援/布局(0.6ms)内呈现fakepath / gems / actionpack-3.2.2 / lib / action_dispatch / middleware / templates / rescues / missing_template.erb

1 个答案:

答案 0 :(得分:1)

错误消息指出,form目录中没有名称为awizard的部分内容。你应该坚持关于部分的Rails约定 - 部分文件总是以下划线为前缀。

在您的情况下,请尝试将form.html.erb重命名为_form.html.erb

编辑:你可以尝试像这样渲染你的部分

render :partial => 'awizard/form'