在 Rails 中使用模型数据填充选择 [英] Populating a Select with Model Data in Rails

查看:45
本文介绍了在 Rails 中使用模型数据填充选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 apply what 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:

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

I have an Image that HABTM Album. To support that, I have an albums_images table with image_id and album_id fields (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.

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

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:

<% 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.

谢谢.

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

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_many albums and an album belongs_to user.

更新:在下面的IV的要求下,此刻,用这个代码:

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

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

我收到此错误:

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

我在第 24 行收到错误.

I get the error in line 24.

推荐答案

嗯,我不确定这是最好的方式,Rails 方式,或者坦率地说,甚至是一种优雅的方式,但这是我使用的代码到目前为止似乎工作正常.

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.

<%= 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.

这篇关于在 Rails 中使用模型数据填充选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆