我想使用my 中的boolean属性在部分 .html 文件中的 html 代码之间切换。这是我的尝试。is_whiteinner_object_inner_object_form_fields
<%= form_for @outer_object do |f| %>
<%= f.fields_for :inner_object do |builder| %>
<%= render :partial => "inner_object_form_fields", :locals => { :f => builder } %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这是我的部分尝试_inner_object_form_fields。
<% if f.is_white == true %>
<%= f.label(:name, "White") %>
<% else %>
<%= f.label(:name, "Black") %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这是 的迁移文件InnerObjects。
class InnerObjects < ActiveRecord::Migration
def self.up
create_table :inner_objects …Run Code Online (Sandbox Code Playgroud) 我在用户和目标之间有一对一的关系.我想构建一个显示用户目标的表单.问题是我的代码仅在用户已定义目标时才有效.没有目标时,不会呈现文本字段.
<%= user_builder.fields_for :goal do |goal_builder| %>
<%= goal_builder.text_field :goal %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
Rails提供了一种简单的方法吗?
在嵌套表单的更新操作期间,它不是更新当前的嵌套记录,而是创建新的嵌套记录。
这是我的控制器的样子:
class ApplicationsController < ApplicationController
before_filter :set_user_and_job
def new
job = params[:job_id]
@application = Application.build(job)
end
def create
@application = Application.new(application_params)
@application.save
redirect_to root_url, :notice => "You have now applied!"
end
def edit
@application = Application.find(params[:id])
@answers = []
@job.questions.each do |question|
@application.answers.each do |answer|
@answers << answer if answer.question_id == question.id
end
end
end
def update
@application = Application.find(params[:id])
@application.update_attributes(application_params)
redirect_to root_url, :notice => "You have updated your application!"
end
def destroy
Application.find(params[:id]).destroy
flash[:success] = "Application Deleted."
redirect_to …Run Code Online (Sandbox Code Playgroud) 在我的数据库中,有很多用户使用无效数据(没有手机号码).因为我刚刚添加了移动存在验证.当我尝试添加新事件时,我收到错误消息"用户移动电话号码必须是...格式".如何在创建事件时跳过用户验证?
event.rb
class Event < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
Run Code Online (Sandbox Code Playgroud)
user.rb
class User < ActiveRecord::Base
has_many :participations
has_many :events, :through => :participations
end
Run Code Online (Sandbox Code Playgroud)
events_controller.rb
def create
@event = Event.new(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to events_path, notice: 'Event was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for @event, :html => {:class => 'row'} do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name …Run Code Online (Sandbox Code Playgroud) 好的伙计们,Rails 5 确实有其细微差别与 Rails 4 不同。我所要做的是,每次我单击表单上的提交按钮时,它都会重新加载错误Profile user must exist and Profile user can't be blank。该表单加载良好,包括嵌套模型表单,但无论出于何种原因,它都无法在尝试将具有以下输出的子模型保存到控制台之前保存父模型:
Puma starting in single mode...
* Version 3.7.0 (ruby 2.2.6-p396), codename: Snowy Sagebrush
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started POST "/users" for 192.168.0.31 at 2017-03-09 18:51:04 -0500
Cannot render console from 192.168.0.31! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
ActiveRecord::SchemaMigration Load (0.2ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by UsersController#create as HTML
Parameters: …Run Code Online (Sandbox Code Playgroud) ruby nested-forms nested-attributes nested-form-for ruby-on-rails-5
在我的product_model_controller.rb强参数中,我有以下代码:
def product_model_params
params.require(:product_model)
.permit(:name, :product_category_id,
product_category_attributes: [:id, :name], attr_val_ids: [])
end
Run Code Online (Sandbox Code Playgroud)
按照它的方式,它工作得很好。但是,如果我更改参数的顺序,它就会停止工作。例子:
def product_model_params
params.require(:product_model)
.permit(:name, product_category_attributes: [:id, :name],
:product_category_id, attr_val_ids: [])
end
Run Code Online (Sandbox Code Playgroud)
错误:
语法错误,意外的 ',',期望 => ..., :name], :product_category_id, attr_val_ids: []) ... ^
为什么会出现这种情况?我已经坚持了很长时间了:/
产品模型.rb
class ProductModel < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: true
has_many :products
has_many :product_model_attr_vals
has_many :attr_vals, through: :product_model_attr_vals
has_many :attrs, through: :attr_vals
belongs_to :product_category
accepts_nested_attributes_for :product_model_attr_vals
accepts_nested_attributes_for :product_category
end
Run Code Online (Sandbox Code Playgroud)
产品类别.rb
class ProductCategory < ApplicationRecord
validates :name, presence: true
validates …Run Code Online (Sandbox Code Playgroud) forms ruby-on-rails nested-forms nested-form-for strong-parameters
我有一个带有表单的广告横幅,第三方网站所有者使用此横幅抓取此代码,并在该网站上显示此横幅.
由于一些使用此横幅的<form>网站在整个网站上都有包装,我发现横幅表单不提交,而是提交外部表单.
<form onSubmit="return alert('outer form submitted')">
<!-- only if i add this empty form it submits the inner -->
<form></form>
<!-- End Empty form -->
<!-- inner form -->
<form onSubmit="return alert('inner form submitted')" >
<input type="submit" />
</form>
<!--end inner form -->
</form>
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,只有我<form> 在内部之前添加一个空,<form>它才会提醒(提交)内部,否则它会提交外部.
为具有表单的网站编写横幅广告的最佳方法是什么?
我正在尝试在Rails 3.0.3中创建一个嵌套的模型表单.这是我的模特:
class Bird < ActiveRecord::Base
has_one :taxon, :as => :organism
accepts_nested_attributes_for :taxon
end
class Taxon < ActiveRecord::Base
belongs_to :organism, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
这是控制器方法:
def new
@bird = Bird.new
@bird.build_taxon
end
Run Code Online (Sandbox Code Playgroud)
这是表格:
New Bird
<% form_for @bird do |f| %>
<p>
<%= f.label :wingspan %>
<%= f.text_field :wingspan %>
</p>
<p>
<%= f.label :body_length %>
<%= f.text_field :body_length %>
</p>
<% f.fields_for :taxon do |builder| %>
<%= builder.label :common_name %>
<%= builder.text_field :common_name %>
<%= builder.label :genus_name %>
<%= …Run Code Online (Sandbox Code Playgroud) 为什么这个:
# edit.html.erb
<%= form_for @product do |f| %>
<%= f.fields_for :shop do |sf| %>
# Nothing here
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
生成隐藏的输入字段:
<input type="hidden" value="23" name="product[shop_attributes][id]" id="product_shop_attributes_id">
Run Code Online (Sandbox Code Playgroud)
?
相关控制器代码:
def edit
@product = Product.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud) 我在rails应用程序中使用嵌套表单,我有一个简单的父子设置:
楷模:
class Experiment < ActiveRecord::Base
has_many :exptypes, :dependent => :destroy
accepts_nested_attributes_for :exptypes, :allow_destroy => true
class Exptype < ActiveRecord::Base
belongs_to :experiment
Run Code Online (Sandbox Code Playgroud)
浏览次数:
家长:
<%= nested_form_for(@experiment) do |f| %>
<%= f.fields_for :exptypes do |builder| %>
<%= render 'exptype_fields', :f => builder %>
<% end %>
<p><%= f.link_to_add "Add an Experimental Type", :exptypes %></p>
Run Code Online (Sandbox Code Playgroud)
儿童:
<h2>Experiment type</h2>
<p>
<div class="field">
<%= f.link_to_remove "Remove this Experiment" %>
</div>
<div class="field">
<%= f.label :type_name %><br>
<%= f.text_field :type_name %>
</div>
</p>
Run Code Online (Sandbox Code Playgroud)
添加许多孩子工作正常,但当我尝试从列表中删除任何(通过单击创建的按钮link_to_remove …
nested-forms ×10
forms ×3
fields-for ×1
hidden-field ×1
html ×1
javascript ×1
partial ×1
ruby ×1
validation ×1