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

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)

Related

rails 4 how to add custom form fields

How can we add custom form fields in rails 4.
For ex:
User (model)
username
email
hashed_password
Now in the form field for password I would like to have fields like password and password confirm.
But due to the introduction of strong parameters in rails 4, I cannot simply send those fields .
It throws error.
It used to be a piece of cake in prior versions of rails i.e in the model we would add our custom form fields to attr_accessor and we could play with them.
NOTE: I tried this thing few weeks ago and it did'nt work, and I don't remember the exact error but was something like 'no method password on user'.
I know it is very stupid of me to not provide you with the exact error messages.
My apologies, can't help I deleted the application.
I was following this tutorial (it's for rails 3) http://www.sitepoint.com/rails-userpassword-authentication-from-scratch-part-i/
Check out How is attr_accessible used in Rails 4? for using strong parameters in rails 4
Protecting attributes is now done in the Controller.
I'm using attr_accessor in model in Rails 4.
In forms I have
<%= text_field_tag 'param_name' %>
And in controller
#model.param_name = params[:param_name]
It works ok and can solve your problem, I think. BUT! I'm not sure it is secure to use it this way. If someone can explain, why it is not appropriate for security reasons and suggest a better way, I'll appreciate it (as the author, I think).

where to put utility code used by a controller and a mailer?

Our app has "notifications" which you view through your inbox on the site, and can also get email to tell you about them. When you receive a notification, it contains a link to reply to the message in question. That might mean sending a PM back to the sender of the original message, or might mean leaving a comment on a post.
This is the code to figure out what reply link to use:
if #notification.post
# comment on the post in question
#reply_link = new_comment_path(:post_id => #notification.post.id)
else
# by default, reply link sends a PM in return
#reply_link = new_notification_path(
:recipient_id => #notification.sender.id,
:subject => #notification.subject =~ /^Re: / ?
#notification.subject :
"Re: " + #notification.subject
)
end
I took that from our controller code, btw: app/controllers/notifications_controller.rb
Now we want to include the same reply link in our email notifications, which means we need to do the same sort of reply link generation in app/mailers/notifier.rb
I don't want to repeat myself, so I would rather create a reply_link method and put it somewhere where both the controller and the mailer can access it.
My first thought was to put it in the model, so that we could have Notification.reply_link. That'd be nice, but it doesn't work because we need new_comment_path and new_notification_path which aren't available in the model.
My second thought was to use a helper, but a) everyone seems to think that helpers suck, and b) we couldn't get it to work anyway.
So, where should I be putting this handy reply_link method, so that it will be accessible to both the controller and the mailer, and in keeping with good coding practices?
Extract it to a module and 'mix it in' to access it in both places you need it.
You can put the module anywhere you like: the lib folder has been a historically popular place, or create a 'modules' folder in your app directory.
Put it in /app/helpers or /lib
I generally tend to put methods accessed from controllers in helpers

Rails 3 mailer for a model without an email attribute

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.

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

What is the best way to show my users a preview of email templates in Ruby on Rails?

My software sends emails for users. I want to show them what the emails will look like before they get sent. However, with ActionMailer conventions, the entire template is in one file. This means the html,head,body tags, etc. Can anyone think of a good way to give my users a preview of what the emails I send out will look like?
Thanks!
I had the same issue. I built out the display with the associated model I was sending rather than in the mailer. I was able to feed sample data or live data to display it to the user.
when it came time to actually send it, I rendered the exact same thing within the mailer view
EDIT:
I apologize for the crap variable names in advance. I am not sure I am allowed to explicitly talk about them :)
Lets say I have a BarMailer function called foo(status,bar)
where status is a test email or a live email and bar is my associated model.
I called deliver_foo("test",bar)
deliver_foo sends out a multipart message so for each part I render_message and pass along variables I need. for example:
p.body = render_message('bar_html', :bar => bar, :other_data => bar.other_data)
so, that render_message is is saying to specifically use the bar_html view (I also have a bar_text for plain text).
this is the contents of my bar_html view:
<%=render :inline => #bar.some_parent.some_other_model.html, :locals => {:other_data => #other_data, :time => Time.now, :bar => #bar }%>
Its a little complicated, but it is based on a template system. By rendering inline everywhere, I am able to use the same code for a number of different functions including previewing and sending. I like this because it becomes a WYSIWIG. No extra code or functionality that could be buggy and muck with the potential output in an email. If it works in one area, it will work in the other. Plus keeping it DRY means I am not going to forget to modify a copy (which I would do frequently, hehe).

Resources