In my RoR application I have a form to create a new email. On this form a user needs to be able to select the email account that they want to use to send the email. This required information is stored in tables User
, Useraccount
, Account
, and Email
as follows.
User Useraccount Account Email
id name user_id account_id id emailaddress id subject account_id
1 Ben 1 2 1 123@abc.com 1 First email 2
2 Gary 1 1 2 456@abc.com 2 Second email 1
3 Abigail 3 2 3 789@abc.com 3 Third email 3
So, when a user creates a new email I want them to be able to select a radio button that represents the email account they want to send the email from, when the select "send" the id of the account should be saved in the account_id
column of the email
table.
In order to do this, the list of a user's accounts needs to be retrieved from Useraccount
and displayed as radio buttons on the form, for example the user Ben would have the option of selecting a radio button for account 2 (456@abc.com) or account 1 (123@abc.com).
So far I have been able to get the radio buttons displayed on the form but when the email is saved the value 0 is saved in the database, so I assume that this is because the radio buttons do not have a value assigned to them. Could someone please help me with this?
My code is as follows.
Emails_controller
:
class EmailsController < ApplicationController
def index
@emails = Email.all
end
def show
@email = Email.find(params[:id])
@recipients = Recipient.where(email_id: @email.id)
end
def new
@email = Email.new
@email.recipients.build
@useraccounts = Useraccount.where(user_id: session[:user_id])
end
def edit
@email = Email.find(params[:id])
end
def create
@email = Email.new(email_params)
if @email.save
redirect_to @email
else
render 'new'
end
end
def update
@email = Email.find(params[:id])
if @email.update(email_params)
redirect_to @email
else
render 'edit'
end
end
def destroy
@email = Email.find(params[:id])
@email.destroy
redirect_to emails_path
end
private
def email_params
params.require(:email).permit(:subject, :message, :account_id, { contact_ids: [] }, { group_ids: [] })
end
end
app/views/email/_form.html.erb
<%= 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 %>
<% @useraccounts.each do |useraccount| %>
<%= f.label :account_id, useraccount.account.email, :value => "true" %>
<%= f.radio_button :account_id, false, :checked => false %>
<% end %>
<p>
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :message %><br>
<%= f.text_area :message %>
</p>
<%= f.collection_check_boxes :contact_ids, Contact.where(user_id: session[:user_id]), :id, :firstname ,{ prompt: "firstname" } %>
</br>
<%= f.collection_check_boxes :group_ids, Group.where(user_id: session[:user_id]), :id, :name ,{ prompt: "name" } %>
<p>
<%= f.submit %>
</p>
<% end %>
In this form it is the lines:
<% @useraccounts.each do |useraccount| %>
<%= f.label :account_id, useraccount.account.email, :value => "true" %>
<%= f.radio_button :account_id, false, :checked => false %>
<% end %>
That display the radio buttons.
My models are:
class Useraccount < ActiveRecord::Base
belongs_to :user
belongs_to :account
end
class Account < ActiveRecord::Base
has_many :emails
has_many :useraccounts
end
class Email < ActiveRecord::Base
belongs_to :email
has_many :recipients
has_many :contacts, through: :recipients, :dependent => :destroy
has_many :groups, through: :recipients, :dependent => :destroy
validates :subject, presence: true, length: { minimum: 1 }
end
class User < ActiveRecord::Base
validates :firstname, presence: true, uniqueness: true
has_secure_password
end
I would much appreciate some help on this.
Aucun commentaire:
Enregistrer un commentaire