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.
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.
Related
I have a rails app with the user model as follows
class User < ActiveRecord::Base
def to_param
"#{username}".parameterize
end
end
In routes.rb I have
resources :users, path: '/u'
To link to the show page of a user, I use
<%= link_to 'Show', #user %>
Now if a user has the username 'UsErNaMe', the above link does not work.
It goes to
http://localhost:3000/u/username
and not
http://localhost:3000/u/UsErNaMe
Any solution to this problem? Thanks in advance!
The usernames in your system should only be case sensitive for display (i.e. someone prefers their username to look a certain way on a page), but never for account lookup. You don't want two accounts, one called "UsErNaMe" and the other called "USERname" to be able to exist at the same time.
I.e. don't worry about the case in the URL. It's not important. What's important is how the username is displayed on the page.
I have implemented ActiveAdmin in my Rails project. I have a resource called "Customer". In the navigation bar, there is an option called "New Customer" to create a customer. How can I change the view of this new customer? When I click "New Customer" button, it has to accept email from me, and it should send an invitation to that email-id.
All you need to do is follow this
https://gist.github.com/mikeatlas/5628317
it has enough details to setup devise_invitable with active_admin.
Actually we can do it by, integrating ActiveAdmin and DeviseInvitable Gems. The following link illustrates that.
Here's a link!, In which they have explained neatly.
If your Customer model has a email field, then you can just allow only the email to be shown in the form, and override the 'create' method. Something like this:
form do |f|
f.input :email
f.actions
end
def create
#send the email here
end
Let's say before a user is allowed to view the creation page of a Model, they are required to accept some terms. The workflow would be like:
User clicks on "create MODEL" -> Brought to a terms page, must accept before moving on -> MODEL creation page
If a user copies the url to the creation page, they should be redirected to the terms page.
What's the best way of going about this? I was thinking of using the session variable somehow...but I can't think of a clever enough idea. Any suggestions?
You can have the following routes:
get 'terms' => 'MODEL#terms'
get 'new' => 'MODEL#terms'
post 'new' => 'MODEL#new'
The "create MODEL" should send a GET request for /terms. Accepting the terms should POST to /new. If the user pasted /new in the URL, he'll be directed to terms instead.
Alternatively (or additionally), you can have the /terms POST a variable, :terms_accepted => true to the MODEL creation page, and on that page, check if :terms_accepted == true. If not, redirect to the terms page.
You can add the acceptance of the terms to the model itself, then you don't need a construction with an extra page and redirect if someone enters in the wrong place. Instead the user can only submit the form for creation when he/she accepts the terms.
You can add the following to the model:
class Model < ActiveRecord::Base
validates :terms_of_service, :acceptance => true
end
And then make sure you have the checkbox for this in the new %{model} form.
Also see: http://guides.rubyonrails.org/active_record_validations_callbacks.html#acceptance and Accept terms of use rails
Please excuse the simplicity and length of this, but I have a little test app that has a users table, with name, email, and salary attributes. I created a mailer that will send out a report of this data to a specific user at my discretion, in other words, when I click a button. My button is created using link_to, and calls an action in my main users_controller, which then calls the mailer action. (I hope that just made sense). It looks like the following and is working just as I hoped; I'm just looking to know if this is the right way to do something like this:
In my users_controller (created by the scaffold generator):
def sendemail
#user = User.find(params[:id])
UserMailer.welcome_email(#user).deliver
redirect_to user_path(#user)
flash[:notice] = 'Email has been sent!'
end
In the user_mailer.rb file:
def welcome_email(user)
#user = user
#url = "http://example.com/login"
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
In the User's show.html.erb page, this is how the email gets sent:
<%= link_to "Send Email", sendemail_user_path(#user) %>
In my routes.rb file, so that everything executes properly (which it does):
resources :users do
member do
get 'sendemail'
end
So, having said all this, it works just like it should. I click on the user's show.html.erb page where I would have the data and charts that I want to eventually display, and at my discretion, I can kick out an email to that user with this data, or whatever I put in the mailer.html.erb file. When it is sent, it flashes the message I specified in the controller, and leaves me on that page, just as I specified; so it's working. I just want to know, is this the right and most ruby/railsy way to do things?
This code looks very similar to the Rails Guides Action Mailer example, so suffice it to say you are creating railsy code.
Also, if your application evolved into a much grander scale you would want to consider delivering emails via a background job to avoid email deliveries from blocking the current thread.
Otherwise the code looks great. Of course, you would most likely send an email after doing something successful in a controller, rather than have a dedicated action for emailing directly. For instance, you might have a welcome action that sends an email on a successful user record save.
I am building an application and it needs to have a feature whereby one user can send another user a message. The system can be the most basic type available in rails and that would suit me fine.
Any ideas on how to go about this?
Thanks.
Table structure like this:
Users
name,pwd
Messages
title,body
UserMessages
user_id,message_id
Why don't you use acts_as_messageable plugin:http://www.philsergi.com/2007/10/actsasmessageable-plugin-released_04.html ?
Similarly, there are other plug-ins for authentication (restful authentication).
So i have implemented the DB tables and now need to pass the data around my system which im finding quite troubling. When the user clicks "send message to" on my form i need it to carry the id of the profile which the user is viewing. I thought this would do that:
<%= link_to "Message", :action => 'message', :id => #user.id %>
Now this pass the persons ID who i was looking to the message action (i know #user.id should work because i use #user.detail to view other details about the user on that page)
My controller should then receive that #user.id, heres my controller:
def message
#reciever = User.find_by_id(params[:id])
end
and in my view for i want to show the recievers id so i thought that
<label>Send Message To: <%= render :text => #reciever.id %></label>
would be suffiencent.
Any ideas?