在表单标记

时间:2017-11-13 16:28:32

标签: ruby-on-rails forms devise simple-form

我正面临一个与通过设计登录生成自动表单有关的奇怪问题。

这是我的观点:

# app/views/devise/sessions/new.html.haml
.jumbotron
  %h2= t('devise.sessions.new')
  = simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
    = f.error_notification
    .form-inputs
      = f.input :email, required: false, autofocus: true
      = f.input :password, required: false
      = f.input :remember_me, as: :boolean if devise_mapping.rememberable?
    .form-actions
      = f.button :submit, t('devise.sessions.new')
  %br/
  = render 'devise/shared/links'

这用于呈现以下内容:

<div id='content'><div class='jumbotron'>
<h2>Accedi</h2>
<form novalidate="novalidate" class="simple_form new_user" id="new_user" action="/users/sign_in?locale=it" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="{authenticity_token}" /><div class='form-inputs'>
<div class="form-group email optional user_email"><label class="control-label email optional" for="user_email">Email</label><input class="form-control string email optional" autofocus="autofocus" type="email" value="" name="user[email]" id="user_email" /></div>
<div class="form-group password optional user_password"><label class="control-label password optional" for="user_password">Password</label><input class="form-control password optional" type="password" name="user[password]" id="user_password" /></div>
<div class="form-group boolean optional user_remember_me"><div class="checkbox"><input value="0" type="hidden" name="user[remember_me]" /><label class="boolean optional" for="user_remember_me"><input class="boolean optional" type="checkbox" value="1" name="user[remember_me]" id="user_remember_me" />Ricordami</label></div></div>
</div>
<div class='form-actions'>
<input type="submit" name="commit" value="Accedi" class="btn btn-default" data-disable-with="Accedi" />
</div>
</form>
<!-- something else -->
</div>
</div>

出于一个非常奇怪的原因,同样的视图现在呈现为:

   <div id='content'><div class='jumbotron'>
        <h2>Accedi</h2>
          <div class='form-inputs'>
          <div class="form-group email optional user_email"><label class="control-label email optional" for="user_email">Email</label><input class="form-control string email optional" autofocus="autofocus" type="email" value="" name="user[email]" id="user_email" /></div>
          <div class="form-group password optional user_password"><label class="control-label password optional" for="user_password">Password</label><input class="form-control password optional" type="password" name="user[password]" id="user_password" /></div>
          <div class="form-group boolean optional user_remember_me"><div class="checkbox"><input value="0" type="hidden" name="user[remember_me]" /><label class="boolean optional" for="user_remember_me"><input class="boolean optional" type="checkbox" value="1" name="user[remember_me]" id="user_remember_me" />Ricordami</label></div></div>
        </div>
        <div class='form-actions'>
          <input type="submit" name="commit" value="Accedi" class="btn btn-default" data-disable-with="Accedi" />
        </div>
      <form novalidate="novalidate" class="simple_form new_user" id="new_user" action="/users/sign_in?locale=it" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="{authenticity_token}" /></form>
        <!-- something else -->
 </div></div>

基本上我的所有form-inputs和我的form-actions现在都在表单标记之外呈现。

您是否知道可能导致此问题的原因 - 因为我一段时间没有改变任何事情?

来自Gemfile

gem 'rails', '~> 5.0.2'
gem 'devise', '~> 4.3'
gem 'simple_form', '~> 3.4'

提前谢谢

更新

如果我检查提交此问题的提交的差异,我会看到更改的文件是:

# $ git diff --name-only master..refactoring-branch 
app/controllers/albums_controller.rb
app/controllers/crud/albums_controller.rb
app/controllers/crud/events_controller.rb
app/helpers/albumizr_helper.rb
app/models/album.rb
app/models/event.rb
app/views/albums/index.html.haml
app/views/layouts/_nav.html.haml
config/locales/layout/navbar.en.yml
config/locales/layout/navbar.it.yml
config/locales/models/album.en.yml
config/locales/models/album.it.yml
config/routes.rb
db/migrate/20171113151343_create_albums.rb
db/schema.rb
spec/factories/album.rb
spec/models/album_spec.rb

我真的没有看到对simple_form应该做什么的直接影响..

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。

当我使用外部服务时,我更喜欢创建构建器,将html代码的生成委托给某些视图(例如图表,iframe等)

在这种情况下,问题出在app/helpers/albumizr_helper.rb

# frozen_string_literal: true

module AlbumizrHelper
  include ActionView::Helpers::TagHelper

  attr_accessor :output_buffer

  def photogallery_item(url)
    # rubocop:disable Rails/OutputSafety
    content_tag :div, class: 'photogallery-item' do
      content_tag(:div, build_albumizr_iframe(url), class: 'embed-responsive-item') 
        content_tag(:div, build_albumizr_button, class: 'text-center')
    end.html_safe
    # rubocop:enable Rails/OutputSafety
  end

  private

  def build_albumizr_iframe(url)
    content_tag(
      :iframe, nil, class: 'iframe-fullscreen', allowfullscreen: '', height: '100%',
                    scrolling: 'no', src: url, width: '100%'
    )
  end

  def build_albumizr_button
    content_tag(:button, class: 'btn btn-primary fullscreen-btn', role: :button) do
      content_tag(:i, nil, class: 'glyphicon glyphicon-fullscreen') 
        ' FullScreen'
    end
  end
end

当我创建包含ActionView::Helpers::TagHelper的某个类时,我设置attr_accessor :output_buffer以获取代码。

在这种情况下,这与简单形式的输出缓冲区产生了一些冲突,因此我在lib/下创建了一个分离的类,一切正常。

我被误导了this answer