简体   繁体   English

Ruby on Rails:显示下拉菜单,其中包含来自其他模型的数据

[英]Ruby on Rails: Display Drop Down Menu with Data from another Model

I have a Ruby on Rails system whereby I have the tables: Users, UserAccounts, Accounts, and Emails with the schemas: 我有一个Ruby on Rails系统,其中有以下表格:用户,用户帐户,帐户和带有模式的电子邮件:

User (id, firstname, surname, password)
UserAccount (user_id, account_id)
Account (id, emailaddress, port, domain)
Email (id, account_id, subject, message)

What I am trying to do is when creating new email, I want all of the user's accounts' emailaddress variable to display on the view in a drop down menu and when the user clicks save I want the account_id to be saved in the Emails table. 我想要做的是在创建新电子邮件时,我希望所有用户帐户的emailaddress变量都显示在下拉菜单的视图中,并且当用户单击“保存”时,我希望将account_id保存在“电子邮件”表中。

At the minute I can display the account_id in a drop down menu on the views/emails/_form.html.erb with the following line of code: 目前,我可以使用以下代码views/emails/_form.html.erb的下拉菜单中显示account_id

<%= f.collection_select :account_id, Useraccount.where(user_id: session[:user_id]), :account_id, :account_id %>

But I cannot change this to show the email address, I have tried the following code to do so: 但是我无法更改它以显示电子邮件地址,我尝试使用以下代码进行操作:

<%= f.collection_select :account_id, Useraccount.where(user_id: session[:user_id]), :account_id, :Account.where(id: account_id).email %>

But this gives me the error undefined local variable or method account_id' for #<#:0x72cde60>` 但这给了我#<#:0x72cde60>`的错误undefined local variable or method account_id'

Can anyone please help? 谁能帮忙吗?

My whole code for the form is: 表格的整个代码是:

<%= form_for @email do |f| %>

  <% if @email.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@email.errors.count, "error") %> prohibited this email from being saved:
      </h2>
      <ul>
        <% @email.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
 <%= f.collection_select :account_id, Useraccount.where(user_id: session[:user_id]), :account_id, :Account.where(id: account_id).email %>
  <p>
    <%= f.label :subject %><br>
    <%= f.text_field :subject %>
  </p>

  <p>
    <%= f.label :message %><br>
    <%= f.text_area :message %>
  </p>

  <p>
    <%= f.submit %>
  </p>

You haven't posted quite enough code to be able to help you very much. 您尚未发布足够多的代码来为您提供很大帮助。 At least need to see the form that you are putting that collection_select on... assuming that it is a form for the new email you are wanting to send? 至少需要查看您要放置该collection_select的表格...假设它是您要发送的新电子邮件的表格?

BTW it is very unusual to have Active Record class names that are plural. 顺便说一句,具有多个复数的Active Record类名称是非常不寻常的。 Breaking Rails naming conventions is a good way to get yourself into confusing situations. 违反Rails命名约定是使自己陷入混乱状态的好方法。

It is easier to do with select instead of collection_select . 使用select而不是collection_select更加容易。 I also assume that you have has_many through association in User model. 我还假设您has_many through用户模型中的关联拥有has_many through

f.select :account_id, User.find(session[:user_id]).accounts.map{ |a| [a.emailaddress, a.id] }

collection_select takes methods names as arguments and it just won't work in your case. collection_select将方法名称作为参数,在您的情况下将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM