我正在尝试在模态窗口中实现railscast(http://railscasts.com/episodes/217-multistep-forms?view=asciicast)。 我在第一页上单击提交后收到致命(异常重新输入)错误消息。我相信它会被卡在某处.next_step。 我正在使用一个调用new.js.erb和的模态窗口。我会根据需要添加其他代码。很抱歉无法正确创建问题。 ---代码:
Model:
estimate.rb
class Estimate < ApplicationRecord
attr_writer :current_step
has_many :estimateitems
def current_step
@current_step || current_step.first
end
def next_step
self.current_step = steps[steps.index(current_step + 1)]
end
def steps
%w[1st 2nd]
end
end
Views:
new.js.erb
// Add the dialog title
$('#dialog h3').html("<i class='glyphicon glyphicon-plus'></i> New Estimate");
// Render the new form
$('.modal-body').html('<%= j render("clients/partials/form_create_estimate") %>');
// Show the dynamic dialog
$('#dialog').modal("show");
// Set focus to the first element
$('#dialog').on('shown.bs.modal', function () {
$('.first_input').focus()
})
form_create_estimate.html.erb
<%= form_for(@estimate, remote: true, :html => { :role => "form" }, :'data-update-target' => 'update-container') do |f| %>
<% if @estimate.current_step = 1 %>
<%= render '1st_step', :f => f %>
<% elsif @estimate.current_step = 2 %>
<%= render '2nd_step', :f => f %>
<% end %>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
Controllers:
class EstimatesController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :edit, :update, :destroy]
before_action :set_estimate, only: [:show, :edit, :update, :destroy]
def index
@estimates = Estimate.all
end
def show
end
def new
@estimate = Estimate.new
end
def edit
end
def create
@estimate = Estimate.new(params[:estimate_params])
@estimate.next_step
render 'new'
end
def update
respond_to do |format|
if @estiamte.update_attributes(estimate_params)
format.json { head :no_content }
format.js
else
format.json { render json: @estimate.errors.full_messages, status: :unprocessable_entity }
end
end
end
def destroy
@estimate.destroy
respond_to do |format|
format.js
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
def set_estimate
@estimate = Estimate.find_by(id: params[:id])
end
def estimate_params
params.require(:estimate).permit(:nickname, :date)
end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end