Rails 3 mailer for a model without an email attribute - ruby-on-rails

I want to create a PageMailer which emails users when someone interacts with one of their pages.
The User has_many pages, and so the page email is defined as user.email
I have tried attr_reader in the Page model, and also I've declared an email method, neither are currently working
I'm getting an error message which states: undefined method 'email' for <Page:0x512000d4>
Can anyone suggest a simple workaround?
thanks
Paul

send_fb_like_email(user, page)
undefined method 'email' for <Page:0x512000d4>
The error says it's trying to find the "email" of a "page".
Looks like you've accidentally switched the user and page params in one of the calls to the method. Go check all the places where you call this email method and see if you've done that.

Related

Customising Whodunnit information for Rails Admin using Paper Trail

I have been able to correctly append the name of a user into the Whodunnit field for actions by adding this to my application_controller
def user_for_paper_trail
current_user ? current_user.name : 'Public user'
end
This works fine.
If a user makes a change using Rails Admin however, the whodunnit is still set to the user id.
Is there a method I can call that would tell paper trail what detail I want to store when a record is being updated via Rails Admin?
RailsAdmin has had some issues with this. You will find the following helpful: Display custom label for User in rails_admin paper_trail history.
Also, because the field is supposed to store IDs (even though it's a string field), I'd keep the IDs and store something nonsensical for the 'Public user' such as '0' (referenced by a constant, of course). I would then override the whodunnit method to return 'Public user' in case super == '0'.
That approach would be particularly helpful if you already have a bunch of versions with IDs in their whodunnit fields or if you have multiple users with the same name.
P.S. To quickly confirm that RailsAdmin is completely ignoring / not ignoring the controller method you've defined, just force-raise an error in the method.

Can anyone show me how to make "Mailboxer"'s controller, model, view, and routes?

First of all, thanks in adavance.
This might be so noob question.
I already did setup devise and mailboxer. devise works completely fine!
Then "username" column is added to Users table, and I configured just like this
/config/initializers/mailboxer.rb
#Configures the methods needed by mailboxer
config.email_method = :email
config.name_method = :username
Now I'm totally stucked!! I don't know how to make the rest for simple message system.
What I want is these message function listed below
index(index/received or index/sent)... you can see the list of messages received(from/to, subjectes, and received date are only shown)
show...you can see whole body of the message.
new... message create form, as an option, you can choose 'reply' from "show" page. in this case body text field already includes messages quotes.
Can someone make very simple example for me?
I can pay up to $50 for it via paypal!
I think I need to make
"messages_controller"
"message" model if you needed with mailboxer gem
"view/messages/index", "view/messages/show", "view/messages/new"
"routes" has to be modified.
Thanks
I assume you've resolved your issues by now, but for future reference, a sample app featuring the mailboxer gem is:
https://github.com/RKushnir/mailboxer-app
(not mine, but helpful to get up and going)

Sending Email with Ruby

I am a rather green RubyOnRails developer. My apologies in advance for the rather simplistic nature of this question.
I am trying to create a page in my RoR app that allows a user to enter the email addresses of friends/family. When the user hits the 'invite' button at the bottom of the form, the app will send invitation emails to the addresses that are entered.
Since the form doesn't corresponding to a model, I can probably use a simple form helper (i.e. form_tag). Also, I've watched the (very good) railscast on how to set up ActionMailer (# 61) and feel comfortable doing this.
My confusion is how to 'link' the form submission (with the email addresses) to the ActionMailer class(es). According to the form description page, I need to specify a named route in the submit tag.
How can I specify a named route for the ActionMailer class(es) and how do I get the user to an acceptable page (i.e. "thank you for the invitations") after they press the 'invite' button.
Thanks in advance!
The form submission has nothing to do with ActionMailer, directly speaking. Just put your ActionMailer call in an action somewhere, and then point your form at that route.
In routes.rb
match '/send_invite' => 'the_controller#send_invite', :as => :send_invite
Point your form to '/send_invite' (or the send_invite_path helper, which will return that string).
Then in "the_controller", define the method "send_invite", and put your ActionMailer code there. Then render a view with your "thank you for the invites" message.
This railscasts is probably the best for learning to send email.
http://railscasts.com/episodes/61-sending-email

Rails 3 + Devise: user_signed_in? for a different user in the database?

I'm building an app where I want to add an online status for a given user.
I know that Devise has a method user_signed_in? built in to check if the user who is using the app is signed in or not. But when I try to use it for a different user like this:
user_signed_in?(user)
user.user_signed_in?
I obviously get an undefined method error.
Does Devise have a method for this or do I have to write my own?
One approach was to store the online status of a given user in the user model.
What's the best solution to this?
I have used Devise on my applications and experienced some of the same problems as you when I first began working with it. You are merely using the helper methods incorrectly. If you'd like to check if the current user has a session and is signed in, you use the helper as such:
if user_signed_in?
which is essentially the same statement as:
if !current_user.nil? && current_user.signed_in
If you'd like to check if a user object is signed in, then you call this: (where user is a User Model Object)
if user.signed_in?
I'm not the author of Devise, but from what I can tell of Warden / Devise neither keep track of who is logged in.
The problem with having an is_online column in the User table is that it is difficult to see who is active on the website. I would add a column to your User model called last_seen as a date-time, and update that with Devise every time the user requests a page. You could then easily add a User.online_count method or also see if a user has been seen at the website in the last 5 minutes.
Use devise_for :user in your routes.rb.

Is it possible to put validations in controllers in Ruby on Rails?

I have an additional member method (and a corresponding route) called download, for which I'd like to validate the existence of a password field.
Putting :validates_presence_of :password in my download method, I get an error claiming it's an undefined method.
Is this possible?
No, you can add errors to the model but validations live in the model. Check out the errors methods and add_to_base if you need to add errors in the controller for whatever reason.

Resources