Ruby-on-rails 在 Rails 中使用模型数据填充选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1477384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:47:57  来源:igfitidea点击:

Populating a Select with Model Data in Rails

ruby-on-rails

提问by Rob Wilkerson

I feel the need to apologize for asking such a simplistic question, but I'm getting increasingly frustrated with the Rails Guides. I'm sure they answer my question, but they don't provide enough context for me to really understand how to applywhat they're giving me. Nor is Google much help, though I may just be searching the wrong terms/phrases. Given that disclaimer, I'm just going to go ahead and ask:

我觉得有必要为问这样一个简单的问题而道歉,但我对 Rails 指南越来越感到沮丧。我确信他们回答了我的问题,但他们没有提供足够的背景信息让我真正理解如何应用他们给我的东西。谷歌也没有太大帮助,尽管我可能只是在搜索错误的术语/短语。鉴于该免责声明,我将继续问:

I have an Imagethat HABTM Album. To support that, I have an albums_imagestable with image_idand album_idfields (no others). For the life of me, I can't figure out how to populate my image form partial so that the user can select the albums a newly uploaded image should belong to.

我有一个ImageHABTM Album。为了支持这一点,我有一个albums_images带有image_idalbum_id字段的表(没有其他的)。对于我的生活,我无法弄清楚如何填充我的图像表单部分,以便用户可以选择新上传的图像应该属于的相册。

I'm learning Rails, so I really just want the basics. I'm sure there are fancy plugins to do this a hundred ways, but I'd like to learn the basics first and build from there. My form partial is pretty much textbook:

我正在学习 Rails,所以我真的只想要基础知识。我敢肯定,有很多很棒的插件可以用一百种方式来做到这一点,但我想先学习基础知识,然后从那里开始构建。我的形式部分几乎是教科书:

<% form_for( @image, :html => { :multipart => true } ) do |f| %>
  # All the basics you'd expect to see.
<% end %>

My most recent attempt doesn't work any better than any other variation I've tried, but it looks like this:

我最近的尝试并不比我尝试过的任何其他变体更好,但它看起来像这样:

<p>
  <%= f.label :album_id %>
  <%= f.select( :album_id, current_user.albums, :id, :name ) -%>
</p>

Again, I recognize the simplicity of the question I'm asking and I've read what I can find, but I haven't been able to put it together into a complete solution. There seem to be many ways to do it, but no real discussion of each one, their pros/cons or how to really use them in a larger context.

再一次,我认识到我问的问题很简单,并且我已经阅读了我能找到的内容,但我无法将其组合成一个完整的解决方案。似乎有很多方法可以做到,但没有真正讨论每一种方法、它们的优缺点或如何在更大的背景下真正使用它们。

Thanks.

谢谢。

UPDATE:A couple of keys to note and a code correction. First, there's a HABTM relationship between images and albums. Neither model table has a FK directly referencing the other. Second, the album collection should be accessed as current_user.albums(corrected above). A user has_manyalbums and an album belongs_touser.

更新:需要注意的几个关键和​​代码更正。首先,图像和相册之间存在 HABTM 关系。两个模型表都没有直接引用另一个的 FK。其次,专辑收藏应按current_user.albums(以上更正)访问。一个用户has_many相册和一个相册belongs_to用户。

UPDATE:At the request of theIV below, at the moment, with this code:

更新:应下面的IV的要求,目前,使用以下代码:

22: <p>  
23:   <%= f.label :album_id %>  
24:   <%= f.select( :album_id, current_user.albums.collect {|a| [a.name, a.id]}) -%>  
25: </p>

I get this error:

我收到此错误:

undefined method `album_id' for #<Image:0x1042ec110>

I get the error in line 24.

我在第 24 行收到错误消息。

采纳答案by Rob Wilkerson

Well, I'm not sure it's the best way, the Rails way or, frankly, even an elegant way, but here's the code I've used that seems to be working so far.

好吧,我不确定这是最好的方式,Rails 方式,或者坦率地说,甚至是一种优雅的方式,但这是我使用的代码,到目前为止似乎有效。

<%= f.label 'Albums' -%>
<%= collection_select( :image, :album_ids, current_user.albums, :id, :name, {}, { :multiple => true } ) -%>

At this point, when I say "working", all I can really attest to is that the page renders with no errors and the appropriate album or albums are selected when I edit an image. I'm still shocked at how difficult it was to cobble together a "full" solution from a lot of disparate sources.

在这一点上,当我说“工作”时,我真正能证明的是页面呈现没有错误,并且在我编辑图像时选择了适当的相册。从许多不同的来源拼凑出一个“完整”的解决方案是多么困难,我仍然感到震惊。

回答by John Topley

I think that selectelements are one of the more confusing aspects of Rails, because as you said there seem to be a number of ways to do it.

我认为select元素是 Rails 中比较令人困惑的方面之一,因为正如您所说,似乎有多种方法可以做到这一点。

Try this:

尝试这个:

<%= f.select(:album_id, @image.albums.all.collect {|a| [a.name, a.id]}) -%>