当我发布表单以创建带有子评论的新查询时(在应用程序中,查询可以有多个评论),评论不会被构建.它在删除状态验证时有效.所以它与构建和保存事物的顺序有关.如何保留验证并保持代码清洁?
(以下是一个示例,因此可能无法完全运行)
车型/ inquiry.rb
class Inquiry < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
Run Code Online (Sandbox Code Playgroud)
车型/ comment.rb
class Comment < ActiveRecord::Base
belongs_to :inquiry
belongs_to :user
validates_presence_of :user_id, :inquiry_id
Run Code Online (Sandbox Code Playgroud)
控制器/ inquiry_controller.rb
expose(:inquiries)
expose(:inquiry)
def new
inquiry.comments.build :user => current_user
end
def create
# inquiry.save => false
# inquiry.valid? => false
# inquiry.errors => {:"comments.inquiry_id"=>["can't be blank"]}
end
Run Code Online (Sandbox Code Playgroud)
意见/查询/ new.html.haml
= simple_form_for inquiry do |f|
= f.simple_fields_for :comments do |c|
= c.hidden_field :user_id
= c.input :body, :label => 'Comment'
= f.button :submit
Run Code Online (Sandbox Code Playgroud)
数据库架构
create_table "inquiries", …Run Code Online (Sandbox Code Playgroud) 我试图改变输入框和标签之间的字体大小和间距,以我用simple_form gem 2.0.2生成的形式.我确定有办法做到这一点?
目前我有:
<%= f.input :comment, :input_html => { :wrap => :soft, :rows => 2}, :label => 'Type in box below:', :item_wrapper_class => 'type' %>
Run Code Online (Sandbox Code Playgroud)
在我的CSS中:
.type{
font-size: 24px;
margin-top: 15px;
}
Run Code Online (Sandbox Code Playgroud)
我在这个网站的帖子中读到'item_wrapper_class'可以解决这个问题,但是当我去Chrome中检查元素时,通过'下面的输入框:'部分,它甚至没有找到.type类,在我的CSS中 - 它只是在bootstrap中转到默认标签类.
任何帮助将不胜感激,谢谢.
我有一些嵌套数据:
@preset = Preset.new
#fields is a postgres json data type
@preset.fields = {'primary_category' => {'category_id' => 57882}}
Run Code Online (Sandbox Code Playgroud)
我希望在params[:preset][:fields]表单提交的POST中保留相同的嵌套结构,所以我在表单部分中有这个:
<%= text_field_tag("preset[fields][primary_category][category_id]",nil) -%>
Run Code Online (Sandbox Code Playgroud)
简单形式不知道如何处理postgres新类型,如hstore或json类型.在我的情况下,我真的不需要它来验证或检测数据类型.有没有办法可以扩展SimpleForm来跳过对列类型的检测,只输出它为textfields输出的相同的现有bootstrap样板,但对于我的任意json嵌套键?
也许是这样的用法:
<%= f.input 'preset[fields][primary_category][category_id]', :as => :json_text_field %>
Run Code Online (Sandbox Code Playgroud)
输出与上面的助手相同的东西,但用标签和控制组分类的div等包围.
我已经研究过根据文档扩展输入基类.
class JsonTextFieldInput < SimpleForm::Inputs::Base
def input
"#{@builder.text_field(???, input_html_options)}".html_safe
end
end
Run Code Online (Sandbox Code Playgroud)
但这里是我迷路的地方,因为我不知道要通过@builder我自己的逻辑来绕过检查属性名称来映射它的哈希键.此外,只更改表单输入而不是标签,这也需要进行一些修改.在任何一种情况下,我都无法走得太远,我可以使用一些指导.
我有一个带有simple_form gem的表单.如果我使用锚标记对浏览器中的网址进行数字处理,则可以:
localhost:3000/contacts/#new_contact #show me the correct form in the page
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
class ContactsController < ApplicationController
def index
@message = Contact.new()
end
def create
@message = Contact.new(msg_params)
@message.request = request
if @message.deliver
redirect_to contacts_path , flash: { success: 'Ok' }
else
render action: :index #how can i render the new_contact anchor??
end
rescue ScriptError
redirect_to contacts_path , flash: { error: 'spam' }
end
private
def msg_params
params.require(:contact).permit(:name,:email,:message,:spam)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的表格:
<%= simple_form_for @message, defaults: { label: false} do |f| %> …Run Code Online (Sandbox Code Playgroud) 我正在使用Enumerize gem https://github.com/brainspec/enumerize/ 它允许我使用siple形式的美丽选择.此选择中的所有选项都已翻译.
en:
enumerize:
user:
sex:
male: 'Man'
female: 'Woman'
Run Code Online (Sandbox Code Playgroud)
所以,在我的形式中,我选择了变体'Man'和'Woman'.当我用'Man'值保存记录时,我会得到具有'male'值的性别属性.现在我想在显示页面上将此值显示为"Man",但是
= @user.sex
Run Code Online (Sandbox Code Playgroud)
输出为"男性"而不是"男性"
在github上的simple_form示例repo中,有一个包含包装器信息的块. https://github.com/rafaelfranca/simple_form-bootstrap/blob/master/app/views/examples/_horizontal_form_sf.html.erb
它工作正常,但似乎需要为每个表单添加许多额外的设置.有没有办法添加名称此信息并将其添加为参数?
<%= simple_form_for @user_horizontal, url: create_horizontal_examples_url, as: 'user_horizontal', html: { class: 'form-horizontal' },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean
} do |f| %>
<%= f.error_notification %>
<%= f.input :email, placeholder: 'Email' %>
Run Code Online (Sandbox Code Playgroud) 在过去,我使用rails 3.x bootstrap 2.x和simple_form取得了巨大的成功.但是,我尝试创建一个新的(未升级的)Rails 4/Bootstrap 3应用程序,虽然simple_form语法能够使用,甚至可以使用嵌套表单,但看起来好像样式不起作用.我在基于非模型的表单上使用了rails-bootstrap-forms,并且样式看起来很好,但是,我更喜欢在我的大多数表单中使用simple_form.
我的文件如下所示
宝石
source 'https://rubygems.org'
gem 'rails', '4.1.6'
gem 'mysql2'
gem 'jquery-rails'
gem 'devise'
gem 'sass-rails'
gem 'coffee-rails', '~> 4.1.0'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
gem 'therubyracer', '0.11.4', :platforms => :ruby
gem 'uglifier', '>= 1.3.0'
gem 'jquery-ui-rails'
gem 'less-rails'
gem 'font-awesome-rails'
gem 'bootstrap_form'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'jquery-turbolinks'
gem 'turbolinks', '1.3.0'
#Forms and user entry
gem 'simple_form'
gem 'cocoon'
gem 'country_select'
gem "ckeditor"
gem "gon"
Run Code Online (Sandbox Code Playgroud)
application.css.scss
/* …Run Code Online (Sandbox Code Playgroud) 我正在使用带有Bootstrap的Rails 4和Simple Form。
我希望我的复选框不会像这样:
<%= c.input :my_bool_field, label: false, class: "form-control" %>
Run Code Online (Sandbox Code Playgroud)
但类似的东西(我有CSS)
<label class="switch switch-primary">
<input type="checkbox" />
<span></span>
</label>
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用simple_form包装器,但是我发现它们有些混乱。有人可以帮我用这种样式创建复选框吗?
所以我的reconciliation模型看起来像这样:
class Reconciliation < ApplicationRecord
belongs_to :location
belongs_to :company
has_and_belongs_to_many :inventory_items
accepts_nested_attributes_for :inventory_items, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
我的InventoryItem模型看起来像这样:
class InventoryItem < ApplicationRecord
belongs_to :product
belongs_to :location, inverse_of: :inventory_items
has_and_belongs_to_many :reconciliations
end
Run Code Online (Sandbox Code Playgroud)
在我看来ReconciliationsController,这就是我的reconciliation_params样子:
def new
@location = Location.find(params[:location_id])
@reconciliation = @location.reconciliations.new
@inventory_items = @location.inventory_items
@start_index = 0
@next_index = @start_index + 1
end
def reconciliation_params
params.require(:reconciliation).permit(:inventory_item_id, :location_id, :display_id, :inventory_items,
inventory_items_attributes: [:id, :quantity_left, :quantity_delivered, :_destroy]
)
end
Run Code Online (Sandbox Code Playgroud)
这是我的相关部分routes.rb:
resources :locations, shallow: true do …Run Code Online (Sandbox Code Playgroud) ruby-on-rails simple-form strong-parameters ruby-on-rails-5 ruby-on-rails-5.1