0

我有一个Ruby on Rails的系统下拉菜单中使用数据,由此我有表:用户,UserAccounts,帐户和电子邮件与架构:Ruby on Rails的:显示从另一个模型

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

我我试图做的是在创建新电子邮件时,我希望所有用户的帐户'emailaddress变量显示在视图中的下拉菜单中,当用户单击保存时,我希望将account_id保存在电子邮件表中。

在一刻起,我可以在下拉菜单中的views/emails/_form.html.erb与下面的代码显示account_id

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

但我不能改变这个显示的电子邮件地址,我曾尝试以下代码可以这样做:

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

但是,这给我的错误undefined local variable or method ACCOUNT_ID”为#<#:0x72cde60>`

任何人都可以请帮忙吗?

我对整个表格的代码是:

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

回答

0

您还没有发布相当足够的代码能够帮助你了。至少需要看到你正在把这个collection_select的表格放在......假设它是你想要发送的新电子邮件的一种形式?

顺便说一句,拥有复数Active Record类名是非常不寻常的。打破Rails命名约定是让自己陷入混乱局面的好方法。

+0

感谢您的建议,我的班级名称不是复数,我只是倾向于多元化讨论中的数据库表名,以便更容易地看到它们包含许多记录。我已经上传了其余的视图,但我不觉得它会告诉你很多。 –

1

select代替collection_select更容易。我还假设你在用户模型中有has_many through关联。

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

collection_select将方法名称作为参数,它不会在你的情况下工作。