【问题标题】:ActiveModel multi-step form not showing next stepActiveModel 多步骤表单未显示下一步
【发布时间】:2012-12-20 21:26:40
【问题描述】:

所以我一直在使用 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

我的看法 - (/views/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 %>

(/views/awizard/_step1.html.erb)

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

(/views/awizard/_step2.html.erb)

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

(/views/awizard/_step3.html.erb)

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

路线代码

resources :awizard

错误信息 单击第一个继续按钮后我收到的错误消息是这样的 - "模板丢失

缺少模板向导/表单,带有 {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]} 的应用程序/表单。搜索:* "fakepath/app/views""

下面显示的更深入的错误日志 -

在 2013-01-07 10:12:06 +1300 开始为 127.0.0.1 放置“/awizard/1” 由 AwizardController#update 处理为 HTML 参数:{"utf8"=>"✓", "authenticity_token"=>"", "awizard"=>{"data"=>"data"}, "step"=>"Continue", "id"=> "1"} 1ms 内完成 500 内部服务器错误

ActionView::MissingTemplate(缺少模板向导/表单、带有 {:locale=>[:en]、:formats=>[:html]、:handlers=>[:erb、:builder、:coffee)的应用程序/表单]}。搜索: *“假路径/应用程序/视图” ): app/controllers/awizard_controller.rb:40:in `update'

在救援/布局中渲染 fakepath/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/missing_template.erb (0.6ms)

【问题讨论】:

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


    【解决方案1】:

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

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

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

    render :partial => 'awizard/form'
    

    【讨论】:

    • 嗨 Qumara,我确定我已经尝试过了,但下次有机会我会再试一次
    • 我试过这个,不,这并不能解决问题我觉得这与路线有关?
    • 当我仔细查看您的路线时,我看到了重复:如果您有resources :awizard,它包含了正确运行的所有要素。尝试注释掉这个post 'awizard/new' =&gt; 'awizard#new' 并发布结果。
    • 还有一个建议:尝试将form.html.erbapp/views/awizard/复制到app/views/。在您的错误日志中,搜索似乎在 views 目录中。但我绝对确定,partials的命名应该在名字前面加上_
    • 您对帖子 'awizard/new' => 'awizard#new' 的看法是正确的我在 /views/awizard 中有 _form.html.erb 我会尝试将它带回 app/views 但我不相信这会解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多