【问题标题】:Simple form styling does not work简单的表单样式不起作用
【发布时间】:2018-10-31 23:28:28
【问题描述】:

我使用 simple_form 启动了一个新应用,但我的表单没有样式。

宝石文件:

source 'https://rubygems.org'

# BACK
gem 'rails', '4.2.7.1'
gem 'pg', '~> 0.15'

# FRONT
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'uglifier', '>= 1.3.0'
gem 'jbuilder', '~> 2.0'
gem 'simple_form'
gem 'slim'
gem 'bootstrap', '~> 4.1.1'
gem 'sprockets-rails', '>=2.3.2'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'rspec-rails', '~> 3.7'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
  gem 'rubocop'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

我的app/assets/stylesheets/application.rb

@import "bootstrap";

表格代码:

.row
  .offset-md-3.col-md-6
    = simple_form_for :user, url: sessions_path do |f|
      = f.text_field :username
      = f.password_field :password

      = f.submit 'Войти'

它会生成

<form novalidate="novalidate" class="simple_form user" action="/sessions" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="authenticity_token" value="...">
  <input type="text" name="user[username]" id="user_username">
  <input type="password" name="user[password]" id="user_password">
  <input type="submit" name="commit" value="Войти">
</form>

看起来像这样:

我可以看到,它至少不会在每个输入周围生成 &lt;div class='form-group'&gt; 等。 我已经运行rails generate simple_form:install --bootstrap。并且可以提供所有需要的信息。

这有什么意义?

【问题讨论】:

  • 您的项目中是否包含引导程序?

标签: ruby-on-rails bootstrap-4 simple-form


【解决方案1】:

Simple Form 使用这种语法:

f.input :username

https://github.com/plataformatec/simple_form

【讨论】:

  • 我有一个。你可以在 Gemfile 中看到它。我现在将进行编辑以显示我的样式表。可能有问题
  • 你使用“f.input :username”时是否有同样的结果,例如?
  • 哇,成功了!我现在在几个项目中工作,所有形式的宝石都在我的脑海中混合在一起%)有什么说我应该使用f.input而不是f.text_field
  • 不错。它在文档中是的,我更新了我的答案
  • 天哪,我怎么没注意到呢?非常感谢!
猜你喜欢
  • 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
相关资源
最近更新 更多