我正在rails 3.1中构建嵌套表单而不是按"添加"按钮我想在空输入字段中键入文本时自动添加输入字段(就像在facebook中提出问题并添加轮询选项一样).但是我也想只添加一个字段,如果在字段中输入了字符,如果删除了字符,则还应删除额外字段.
我正在使用rails 3.1,所以jQuery和jquery-rails包含在我的gem中,我还不知道咖啡脚本.
我使用rails 4 nested_form为我的模型创建一个表单.模型相册有很多图像.上传图片的部分表格如下:
<div class="field">
<%= f.fields_for :images do |image_form| %>
<div>
<%= image_form.file_field(:image_file) %>
<%= image_form.link_to_remove "Remove this image" %>
</div>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
如何在编辑表单上为每个图像显示一个拇指?目前只有编辑时出现浏览按钮.
Update2:我已经清理了代码,这似乎解决了一些问题。我已将新代码作为新问题发布在此处。
更新:组织和用户有一对多的关系。我的问题涉及需要组织和用户的联合注册表单。在 maxcal 对原帖的帮助之后,我create为我的嵌套表单(“组织有很多用户”)编写了一个新方法,如下所示。我也添加begin...rescue...end到create方法中。现在的情况/问题:
有人知道代码有什么问题吗?问题似乎更多是嵌套用户而不是组织(父)。此外,users_attributes.empty?不起作用,因为根据日志,空提交的表单仍然包含此类属性:
Parameters: {"utf8"=>"?", "authenticity_token"=>"***", "organization"=>{"name"=>"", "bag"=>"", "users_attributes"=>{"0"=>{"email"=>"", "username"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "usertype"=>"2", "admin"=>"true"}}}, "commit"=>"Register"}
Run Code Online (Sandbox Code Playgroud)
.
def create
@organization = Organization.new(new_params.except(:users_attributes))
begin
if users_attributes.empty?
@organisation.errors.add(:users, 'No user provided')
end
@organization.transaction do
@organization.save!
if users_attributes.any?
@organization.users.create!(users_attributes)
end
end
rescue ActiveRecord::RecordInvalid => invalid
if @organization.persisted?
if @organization.users.any?
@organization.users.each do |single_user|
single_user.send_activation_email
end
end
flash[:success] = "Confirmation email sent."
redirect_to root_url
else
@organization.users.build if @organization.users.blank?
render …Run Code Online (Sandbox Code Playgroud) 我有一个 Vendor 模型、一个 Product 模型和一个 VendorProduct 模型,具有以下关联
class Vendor < ActiveRecord::Base
has_many :vendor_products
has_many :products, through: :vendor_products
end
class Product < ActiveRecord::Base
has_many :vendor_products
has_many :vendors, through: :vendor_products
end
class VendorProduct < ActiveRecord::Base
belongs_to :vendor
belongs_to :product
end
Run Code Online (Sandbox Code Playgroud)
我正在使用nested_form gem 在我的供应商 _form.html.erb 页面上显示产品的下拉集合选择选项
<%= nested_form_for(@vendor) do |f| %>
<% if @vendor.errors.any? %>
:
:
:
<%= f.fields_for :vendor_products do |vproducts| %>
<%= render 'product_fields', :f => vproducts %>
<%= vproducts.link_to_remove "Remove this Product" %>
<% end %>  
<%= f.link_to_add …Run Code Online (Sandbox Code Playgroud) 我正在使用 Rails 5 及其最新稳定版本的所有内容。所以我得到以下信息:
\n\n\n\n\n您已将关联设置为必需,但它丢失了。\n 在 Rails 5 中,默认情况下将关联设置为必需,因此,如果您想要\n 将其保留为空,则需要在\n 模式下对关联设置可选项:true
\n
这太棒了,我明白发生了什么,但是在我的一生中,我无法弄清楚如何让父模型首先保存,因此 user_id 被转换为嵌套模型记录。我在上面到处都看到了相同的答案,但是除了将初始化程序中的默认值从 true 更改为 false 之外,没有人解释解决方法。这并不能解决问题,因为记录确实保存了,但不包含 user_id。
\n\n下面是我的代码库,我想问而不是用上面的引用来回应,有人可以启发我如何在保存时将 USER_ID 字段放入嵌套属性中。我拒绝禁用验证并手动处理插入,因为这不是 ruby 方式并且违反了标准!\n提前感谢任何可以直接回答这个问题并且没有偏离 ruby 方式的模糊解释的人!
\n\n###Models\n#Users\nclass User < ApplicationRecord\n has_one :profile, inverse_of: :user\n accepts_nested_attributes_for :profile, allow_destroy: true\nend\n\n#Profiles\nclass Profile < ApplicationRecord\n belongs_to :user, inverse_of: :profile\nend\n\n###Controller\nclass UsersController < ApplicationController\n before_action :set_user, only: [:show, :edit, :update, :destroy]\n\n # GET /users\n # GET /users.json\n def index\n @users = User.all\n end\n\n # GET /users/1\n # GET /users/1.json\n def …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails nested-forms nested-attributes ruby-on-rails-5
使用 devise 和acts_as tennant 构建 Rails 5 应用程序。
不太确定我哪里出错了,我试图在帐户/新下以相同的形式创建帐户和帐户的所有者。
目前我收到以下错误:
/accounts/new 处的 ArgumentError 未找到名称“user”的关联。已经定义了吗?
我已经检查了我的模型和控制器,但似乎无法弄清楚这一点。
账户.rb
class Account < ApplicationRecord
RESTRICTED_NAMES = ["www", "admin", "loadflo"]
has_many :users
before_validation :downcase_name, :create_account_name
strip_attributes only: :account_name, regex: /[^[:alnum:]_-]/
validates :user, presence: true
validates :name, presence: true,
uniqueness: {case_sensitive: false},
exclusion: { in: RESTRICTED_NAMES, message: "This is a restricted name. Please try again or contact support." }
accepts_nested_attributes_for :user
private
def downcase_name
self.name = name.try(:downcase)
end
def create_account_name
self.account_name = self.name
end
end
Run Code Online (Sandbox Code Playgroud)
账户控制器.rb …
我觉得这在Rails中应该是一件很容易做到的事情,但是Rails中所有嵌套表单的示例都没有考虑到大多数嵌套表单在通过嵌套表单创建新对象时也需要传递current_user这一事实。
目前我可以让它发挥作用的唯一方法是传递一个隐藏字段,例如<%= form.hidden_field :user_id, value: current_user.id %>.
对于我的具体示例,我有一个名为“结果”的模型,它有许多“课程”,我想通过结果表单创建新课程,而不传递隐藏的:user_id.
这似乎不安全,因为有人可以在浏览器中编辑该隐藏字段,然后提交表单,从而将提交与不同的用户相关联。current_user.id 似乎是您不希望作为隐藏字段嵌入到 html 中的类型。
那么如何在嵌套对象和 current_user 之间创建关联而不将隐藏字段放入表单中呢?
仅供参考,我使用GoRails 嵌套表单和刺激样式 javascript 来添加和删除结果表单中的课程。(这是该示例的源代码。)以下是我的代码的相关部分:
模型/结果.rb
class Result < ApplicationRecord
belongs_to :user
has_many :lessons, inverse_of: :result
accepts_nested_attributes_for :lessons, reject_if: :all_blank, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
模型/lesson.rb
class Lesson < ApplicationRecord
belongs_to :user
belongs_to :result
end
Run Code Online (Sandbox Code Playgroud)
控制器/results_controller.rb
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
def new
@result = Result.new
@result.lessons.new
end
def create
@result = current_user.results.new(result_params)
respond_to …Run Code Online (Sandbox Code Playgroud) 在rails 2.3.14视图中,我有一个fields_for嵌套在form_for中,数据显示在HTML表中.在field_for中我有几个模型属性,我想要显示为简单的静态文本,而不是表单字段.所有数据都在text_field中正确显示,但我无法想象将其显示为纯文本.我知道这一定是一个愚蠢的问题,但我无法弄明白,每个表单示例都只有表单字段.
目标:保存构思模型的评论.
形成:
<%= form_for([@idea, IdeaComment.new], :validate => true) do |f| %>
<div class="control-group">
<div class="controls">
<%= f.text_area :text, :placeholder => 'some text', :rows => 5 %>
<%= validate_errors(IdeaComment.new) %>
</div>
</div>
<%= f.button 'Comment', :class => 'button grad-green', :type => 'submit' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
控制器:
@idea_comment = IdeaComment.new(params[:idea_comment])
...
Run Code Online (Sandbox Code Playgroud)
但是如果我们看看params hash:

如何将idea_id传递给"idea_comment"?
我已经查看了有关stackoverflow和其他地方这个相当常见的问题的所有帖子,但我还没有找到答案.基本上我的嵌套表单没有构建,因此在我显示页面时不可见.
这是我的用户控制器的相关部分,users_controller.rb:
def new
@user = User.new
@user.build_user_account
end
Run Code Online (Sandbox Code Playgroud)
这是我user.rb文件中的相关部分:
class User < ActiveRecord::Base
has_one :user_account, :class_name => "UserAccount"
accepts_nested_attributes_for :user_account
Run Code Online (Sandbox Code Playgroud)
我的user_account.rb档案:
class UserAccount < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
这是我的_form.html.erb档案:
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.text_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br>
<%= f.text_field :password_confirmation %>
</div>
<% f.fields_for :user_account, @user.user_account do |user_account| %>
<div class="field"> …Run Code Online (Sandbox Code Playgroud) nested-forms ×10
ruby ×5
activerecord ×1
erb ×1
has-one ×1
javascript ×1
jquery ×1
paperclip ×1
validation ×1