验证后,我得到一个错误,我返回到 :action => :new
。
表单上的某些字段已经填写,所以即使在出现错误消息后我也想保留它们。
如何实现?
最佳答案
您的 View (new.html.erb) 如下所示
<%= error_message_for :user %>
<% form_for :user, :action=>"create" do|f|%>
<%= f.text_field :login %>
<% end %>
Controller 代码(创建方法)
def create
@user=User.new(params[:user])
if @user.save
redirect_to :action=>'index'
else
render :action=>'new' #you should render to fill fields after error message
end
end
关于ruby-on-rails - 出错后保持填写表单字段 (RoR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916995/