ruby-on-rails - 如果表单错误,带有jQuery选择字段的简单表单将无法正常工作

标签 ruby-on-rails jquery-plugins error-handling simple-form jquery-chosen

我在Rails中有一个表单的多选输入字段。除非用户提交了模型中的验证器不接受的值,否则它可以完美地工作。当页面重新呈现并显示错误消息时,表单字段将从多选字段更改为文本字段。我认为问题是Rails由于某种原因更改了CSS类。这是加载表单实例变量的 Controller :

def edit
    @skills = Skill.all.collect {|skill| skill.label}
    @profile = current_user.profile
end

形式如下:
<%= simple_form_for(@profile, :html => { :method => :put }) do |f| %>
  <%= f.error_notification %>
        <%= f.input :tag_list, collection: @skills, 
          input_html: {class: 'chosen-select', multiple: true, 
          style: 'width: 390px; height: 40px; border-radius: 5px;'},  
          placeholder: 'Tags (seperated by commas)', label: "-" %> <br />
        <%= f.submit "Save" %>
<% end %>

这是更新 Controller :
def update

    @profile = current_user.profile

    begin
      if @profile.update_attributes!(profile_params)
        flash[:notice] = "Successfully updated profile."
        redirect_to profile_path(current_user.profile_name)
      else
        render :action => 'edit'
      end
    rescue
      render :action => 'edit'
    end
end

第一次重新设置表单时,它可以完美地工作。为该字段生成的html如下所示:
<div class="input select optional profile_tag_list">
  <label class="select optional" for="profile_tag_list">-</label>
  <input name="profile[tag_list][]" type="hidden" value="" />
    <select class="select optional chosen-select" id="profile_tag_list" 
      multiple="multiple" name="profile[tag_list][]" 
      placeholder="Tags (seperated by commas)" 
      style="width: 390px; height: 40px; border-radius: 5px;">
         <option value="Finance">Finance</option>
         <option value="PHP">PHP</option>
         <option value="python">python</option>
    </select>
</div>

但是,当更新操作失败后呈现时,html如下所示:
<div class="input string optional profile_tag_list field_with_errors">
   <label class="string optional" for="profile_tag_list">-</label>
   <input class="string optional chosen-select" id="profile_tag_list" 
     multiple="multiple"  name="profile[tag_list][]" 
     placeholder="Tags (seperated by commas)" 
     style="width: 390px; height: 40px; border-radius: 5px;" type="text"
     value="PHP" />
       <span class="error">Please only submit skills from the list.</span>
</div>

重新提交表单后,如何使表单仍显示选择字段?谢谢。

最佳答案

由于每个请求都是无状态的,因此您需要在返回编辑表单之前重建@skills实例变量。另外,您通常不希望使用“!”在update_attributes调用中,因为如果发生任何验证错误,它将说引发RecordInvalid错误。您可以使用:

if @profile.update_attributes(profile_params)
  flash[:notice] = "Successfully updated profile."
  redirect_to profile_path(current_user.profile_name)
else
  @skills = Skill.all.collect {|skill| skill.label}
  render :action => 'edit'
end

如果实例变量设置变得复杂,则可以将其移到editupdate方法都使用的共享方法中。而且,如果您遇到了RecordInvalid以外的其他类型的错误,则可以考虑使用begin/rescue结构。

关于ruby-on-rails - 如果表单错误,带有jQuery选择字段的简单表单将无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19916441/

相关文章:

ruby-on-rails - rails : Including a Concern with a constant within a Concern

jQuery UI 日期选择器 : how to add clickable events on particular dates?

jquery - 设置 jstree 时要包含哪些 .js 和 .css 文件

error-handling - Redux-全局错误处理程序

ruby-on-rails - 将 acts_as_taggable 移动到包含的模块中时出错

ruby-on-rails - 如何回滚到以前的 Gemfile.Lock?

ruby-on-rails - 在 Ruby on Rails 中持久化购物车

javascript - 黑客 : Disable click click with jQuery

c - 防止不断检查错误的模式?

python - 将货币对象更改为小数。使用小数实例