问题
I been struggling of thinking of a way to complete this function.
Currently I have a user with a profile. And a general Contact form model that is table-less and doesn't save anything to the database.
My goal is to have a general contact form, In which I can link a contact button on a individual user profile. That contact form when submitted will be sent to the user email specified in the profile attribute. So for example the profile has a field t.string contact_email.
Currently I have the contact model set up where it can send to one individual email. Mainly the app owner.
class ContactMailer < ApplicationMailer
default :to => "stephen@example.com"
def contact_me(msg)
@msg = msg
mail from: @msg.email, subject: @msg.subject, body: @msg.content
end
end
My goal is to simply link the
default :to => "stephen@example.com"
to something like
default :to => "@profile.contact_email"
I have no idea how to specify the user for the form or if its possible. The process would have to include once the visitor hits contact us the form takes the profile email and uses it as the recipient.
Sorry if this seems like I brung nothing to table to answering I'm just looking for a tip on maybe where to start or how it could be done.
回答1:
Note: Don't get into this thing that you would have to specify to
in default. You can simply send to
in mail
method like you are sending from
, subject
and other variables.
There are several ways to do it:
1) You can use hidden_field_tag
to pass along the email of that user like following
<%= hidden_field_tag :contact_email, @user.contact_email %>
And then on server side, you can have its value through parmas[:contact_email]
.
2) You can send the email of the user when you can call contact_me
. Right now, you are only sending one parameter msg
. So,
def contact_me(msg, to)
@msg = msg
mail to: to, from: @msg.email, subject: @msg.subject, body: @msg.content
end
And when you call contact_me
, you can do: ContactMailer.contact_me(msg, @user.contact_email)
though it may be different depending upon the actual implementation in your code, but the concept would always be same.
来源:https://stackoverflow.com/questions/36690845/contact-form-for-user-profile