我是rails的新手,我在使用collection_select保存连接表的嵌套属性时遇到问题.我有模型post,tag和post_tagging.post_tagging是一个连接表.
我想为每个帖子设置多个标签,所以我尝试通过collection_select使用多选,但是当我保存时,只将post_id插入到数据库中.下面是我的代码和日志.
Post.rb
class Post < ActiveRecord::Base
has_many :post_taggings, foreign_key: :post_id, dependent: :destroy
has_many :tags, through: :post_taggings, source: :tag
accepts_nested_attributes_for :post_taggings, reject_if: :all_blank, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
Tag.rb
class Tag < ActiveRecord::Base
has_many :post_taggings, foreign_key: :tag_id, dependent: :destroy
has_many :posts, through: :post_taggings, source: :post
end
Run Code Online (Sandbox Code Playgroud)
post_tagging.rb(我在post_tagging模型中关闭了tag_id和post_id的状态验证,因此我可以获得POST的日志.)
class PostTagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
#validates :post_id, presence: true
#validates :tag_id, presence: true
end
Run Code Online (Sandbox Code Playgroud)
posts_controller.rb(缩写)
class PostsController < ApplicationController
def new
@post = Post.new
@post.post_taggings.build
end
def new_post_params
params.require(:post).permit(:title, post_taggings_attributes: { :tag_id …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails nested-attributes ruby-on-rails-4 collection-select
我正在尝试使用帮助程序collection_radio_buttons将图像显示为标签,并希望将image_id保存到模型中,以便我可以检索url,到目前为止,我已经提出了一个显示单选按钮和image_url为标签
我已经注意到的是,我只能单击一次单选按钮,然后它才保持在打开状态
<%= f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) %>
Run Code Online (Sandbox Code Playgroud)
生成的html看起来像这样
<input id="campaign_default_image_id_1" name="campaign[default_image_id]" type="radio" value="1" />
<label for="campaign_default_image_id_1">https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/1/vandals_portfolio.png</label>
Run Code Online (Sandbox Code Playgroud)
如何正确设置?我可以将标签包裹在image_tag中吗?
任何帮助表示赞赏
编辑
因此,在经过更多谷歌搜索并将其拼凑在一起之后,我现在可以渲染图像和单选按钮,但是该图像是我通过载波上传的默认图像,而不是我想要的调整大小的小版本。我可以通过:小版本通过?
<%= f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) do |b| %>
<%= b.label { image_tag("#{b.text}") + b.radio_button } %>
Run Code Online (Sandbox Code Playgroud)
b.text检索网址
https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/2/ymca_portfolio.png
Run Code Online (Sandbox Code Playgroud)
但是我想用fancy_box_array为文件名加上前缀,就像这样
https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/2/fancy_box_array_ymca_portfolio.png
Run Code Online (Sandbox Code Playgroud)
用此新代码生成的html是
<label for="campaign_default_image_id_1"><img alt="Vandals portfolio" src="https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/1/vandals_portfolio.png" />
<input id="campaign_default_image_id_1" name="campaign[default_image_id]" type="radio" value="1" /></label>
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个rails4应用程序.目前,我的收藏选择仅在我选择一个选项时才有效.以下是我的工作代码.我只有产品表格.行业模型填充seeds.rb.IndustryProduct仅用于连接其他2个型号.
我想知道我必须在代码中更改哪些内容才能选择更多内容.
我看到一些工作示例的multiple: true选项如(https://www.youtube.com/watch?v=ZNrNGTe2Zqk 10:20)但在这种情况下,用户界面有点难看+无法用任何样本将其拉下来码.是否还有其他解决方案,例如选择一个选项而不是一个包含多个选项的方框?
楷模:
class Product < ActiveRecord::Base
belongs_to :user
has_many :industry_products
has_many :industries, through: :industry_products
has_many :product_features
accepts_nested_attributes_for :industry_products, allow_destroy: true
accepts_nested_attributes_for :product_features
validates_associated :industry_products
validates_associated :product_features
end
class Industry < ActiveRecord::Base
has_many :industry_products
has_many :products, through: :industry_products
accepts_nested_attributes_for :industry_products
end
class IndustryProduct < ActiveRecord::Base
belongs_to :product
belongs_to :industry
end
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for @product do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
......
<%= f.fields_for :industry_products do |p| %>
<%= p.collection_select …Run Code Online (Sandbox Code Playgroud) forms activerecord ruby-on-rails nested-attributes collection-select
我有一个collection_select下拉列表,其下拉名称如下:
<%= f.collection_select(:person_id, Person.all, :id, :name) %>
Run Code Online (Sandbox Code Playgroud)
但我有一个指向他们所属团体的人的外键.在下拉列表中,我想显示人员名称和他们旁边的组,如下所示:
保罗(高尔夫球手)凯文(水手)
等......
这可以使用collection_select吗?
在Rails 4中,我有
<%= form_for @person do |f| %>
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: "Select your country" %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
每当加载页面时,我都希望"选择你的国家/地区"被选为默认值.一种方法是使用javascript(在加载dom后选择它).是否有更简单的方法来添加选项collection_select?
谢谢.
我在创建一个通过关联保存我的 has_many 的表单时遇到问题。我已经通过发布 json 成功保存了,但表单对我来说还不起作用。表单提交创建的请求参数将无法正常工作。任何帮助我找到解决方案的帮助都会帮助我避免在这方面浪费更多时间。谢谢前面。
编辑 - 添加了 forms_for 尝试和创建的 params json,但在底部效果不佳 -
Json post 请求参数有效:
{
"author": {
"name": "Author Name",
"post_authors_attributes": [
{"post_id":"1"},
{"post_id":"2"},
{"post_id":"3"}
]
}
}
Run Code Online (Sandbox Code Playgroud)
Rails 表单生成的参数不保存。
{
"author": {
"name": "assd",
"post_authors_attributes": [
"",
"2",
"3"
]
}
}
Run Code Online (Sandbox Code Playgroud)
...以及相关的代码示例...
作者模型
class Author < ActiveRecord::Base
has_many :post_authors
has_many :posts, :through => :post_authors
accepts_nested_attributes_for :post_authors
end
Run Code Online (Sandbox Code Playgroud)
帖子模型(目前仅适用于作者有很多帖子,而不是相反)
class Post < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
帖子作者模型
class PostAuthor < ActiveRecord::Base
belongs_to :post
belongs_to :author
end …Run Code Online (Sandbox Code Playgroud) 我有 Rails 应用程序,其用例是在 collection_select 下拉框中显示价格。例如,产品名称及其价格,或客户和欠款金额。
我很清楚,简单地包含相关帮助程序来访问 number_to_currency 等函数是不好的 MVC 实践,但我可以使用产品或支付模型上的自定义方法获得一个看起来像 20.2 美元的值。因此,它是可读的,但不是最佳的,因为系统的大多数用户都期望 20 美元 20 美分表示为 20.20 美元。
有人对轻松自定义 collection_select 的 text_method 有任何建议吗?
我正在尝试制作一个collection_select下拉列表,其中包含来自另一个模型的字段值。我得到了以下 2 个模型:
Documents:
class CreateDocuments < ActiveRecord::Migration[5.0]
def change
create_table :documents do |t|
t.string :etiquette_number
t.string :etiquette_type
t.boolean :important
t.string :work_text
t.integer :user_id
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
Entries:
class CreateEntries < ActiveRecord::Migration[5.0]
def change
create_table :entries do |t|
t.integer :document_id
t.integer :user_id
t.string :work
t.date :date
t.integer :time
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想在document_id(Entries模型中)上进行下拉选择,我可以在其中选择文档的 id 值。
到目前为止我得到了这个,但我不确定它是否是正确的方法
models/document.rb
class Document < ApplicationRecord
has_many :Entries
end
Run Code Online (Sandbox Code Playgroud)
models/entry.rb
class Entry < ApplicationRecord
belongs_to :Documents …Run Code Online (Sandbox Code Playgroud) 正如您在下面看到的,我创建了一个哈希,但我不知道在我的 collection_select 标记中引用该哈希。所以我已经成功地做到了这一点,但我的哈希是配置文件对象的集合,当我尝试使用键值对的集合来执行此操作时,它似乎不起作用,我将首先向您展示正确工作的代码,然后我将向您展示不起作用的代码。
这给了我零错误:
<% listoflos = [] %>
<% @profiles.each do |profile| %>
<% listoflos.push(profile) if profile.title == "loan officer" %>
<% end %>
<%= f.collection_select :loanofficer_id, listoflos, :user_id, :firstname, {prompt: true} %>
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
<%= f.label "Progress" %> 
<% listofprogress = [["1 Not contacted", "1"],["2 Interested", "2"],["3 App Taken", "3"],["4 Priced", "4"],["5 Disclosure Signed", "5"],["6 No Appraisal Needed", "6"],["7 Appraisal Ordered", "7"],["8 Appraisal Recieved", "8"],["9 In Underwriting", "9"],["10 Closing Scheduled", "10"],["11 Closed", "11"],["12 Dead", "12"],["Unknown", "unknown"]] %>
<%= f.collection_select …Run Code Online (Sandbox Code Playgroud) 这段代码全部基于Michael Hartl的Ruby on Rails教程.我创建了一个assigned在我的动作users_controller与
def assign
puts "in assign..."
@scout = User.find(params[:follower_id])
@der = User.find(params[:followed_id])
end
Run Code Online (Sandbox Code Playgroud)
我意识到这目前没有做任何事情,但在_follow部分方面,我有
<%= form_for @user, url: { action: "assign" } do |f| %>
<div><%= hidden_field_tag :follower_id, @user.id %></div>
<%= f.label :followed_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误No route matches {:action=>"assign", :controller=>"users", :id=>"4"}.我是rails的新手,所以这可能只是一个愚蠢的问题.我究竟做错了什么?我需要修改我的routes.rb文件吗?另外,如果我尝试过<%= form_for @user do |f| …
我有一个 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) 我正在使用表单在 RoR 中创建记录。我有下拉菜单,但我不知道如何添加用于设置此元素样式的类。
代码:
<%= f.collection_select :id, TipusProducte.order(:nom),:id ,:nom, include_blank: false, class: "btn btn-secondary dropdown-toggle" %>
Run Code Online (Sandbox Code Playgroud)
但是这个类不起作用,这个样式来自Bootstrap。
谢谢!
ruby-on-rails ×11
ruby ×5
forms ×3
activerecord ×1
arrays ×1
css ×1
form-for ×1
hash ×1
html ×1
nested-forms ×1
radio-button ×1
select ×1
selected ×1