ActionMailer debugging - ruby-on-rails-3

I'm trying to use Action Mailer in Rails to send email to users.
def create
...
if #post.save
UserMailer.archive_confirmation(#site).deliver
end
end
But when I try it, I don't get any email, and I have no idea how to debug (since the create method runs successfully, and everything else goes as expected, there's no error message) Where could ActionMailer go wrong?

Check your RAILS log, APP_ROOT/log/development.log or APP_ROOT/log/production.log.

If I am writing the code to sent the email in controller i can write like these to handle the rescue in my application and i can display the error message that is come in rescue we can display.
I'm using something like this in the controller:
if #user.save
begin
UserMailer.welcome_email(#user).deliver
flash[:success] = "#{#user.name} created"
rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
flash[:notice]=e.message
end
redirect_to home_index_path
end

In your respective environment file:
## config/environments/development.rb
config.action_mailer.raise_delivery_errors = true

Related

Where can I turn off paperclips validation with save(false) command

I have a blog application for Rails 5. It has a file attachment, which is not correctly validated by paperclip 5.1.0 gem. So I want to turn validation off in paperclips save method. How to do that?
I have articles_controller with:
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
...
private
def article_params
params.require(:article).permit(:title, :text, :attachment)
end
end
Where do I change paperclips save method to save(false) in order to turn off validation of attachments.
The command do_not_validate_attachment_file_type :attachment does not work properly.
From the comment on git issue:
i've just discovered that for do_not_validate_attachment_file_type to stop the validation you have to add an initializer in config/initializer/paperclip.rb
with
Paperclip::Attachment.default_options[:validate_media_type] = false
I couln't find this in the docs only in the NEWS file

Redirect rails app to home page on crash

I want my app to redirect to my home page ie posts#index. It is a rails2 app which I am trying to migrate to rails 3.
def rescue_action_in_public(exception)
flash[:notice] = "There was an error. Please try again." # #{exception}
redirect_to :controller => :posts, :action => :index
end
This method I presume does this task. How ever, It won't work in rails 3 and I see the 'Sorry something went wrong!' page
How can I get this functionality working in rails 3? If any more info is, needed I am willing to paste here.
in rails 3 try this
def rescue_action_in_public(exception)
status = status_code(exception)
locale_path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
path = "#{public_path}/#{status}.html"
if locale_path && File.exist?(locale_path)
render(status, File.read(locale_path))
elsif File.exist?(path)
render(status, File.read(path))
else
render(status, '')
end
end
from apidock
You can right this way!
def rescue_action_in_public(exception)
flash[:notice] = "There was an error. Please try again." # #{exception}
redirect_to posts_path
end

find_by_* method is not returning the object

I am trying to set up a simple authentication for my rails application. I have a security_users scaffold and have created some users.
When, I am trying to log in using some of these accounts it seams that the "find_by_*" method is not able to detect the current one.
This is how my session controller looks like (I have comment the password check in purpose in order to debug the issue):
class SessionsController < ApplicationController
def new
end
def create
#security_user = SecurityUser.find_by_email(params[:email])
if #security_user #&& #security_user.authenticate(params[:password])
session[:security_user_id] = #security_user.id
redirect_to root_url, notice: "Logged in!"
else
flash.now.alert = "Email or password is invalid"
render 'new'
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end
end
So, when I try to create a session (to log in) I am redirect to the session 'new' template. This is the debug information:
which seems to be all right. Why the following statement could not find the record:
SecurityUser.find_by_email(params[:email])
EDIT:
When I entered the line above in the console it is returning the record:
First off, unless this is a simple exercise in Rails authentication, you should use Devise or AuthLogic at this stage.
Second, are you sure that params[:email] contains the email you are looking for? From your params, it looks to me like you want to use params[:session][:email].
Third, you should move this down into the model. For example:
class SecurityUser < ActiveRecord::Base
def self.authenticate(params)
user = where(email: params[:email]).first
(user && user.password == params[:password]) ? user : false
end
end
And in the controller:
#user = SecurityUser.authenticate params[:session]
session[:user_id] = user.id if #user
Note above that the password is not hashed - you should not save a plain text password - but that's not what this is about.
Also note that now you should use where().first instead of find_by.

respond_with is redirecting to specified location even on validation errors, in rails3

When using location in respond with, it is ignoring validation errors and redirecting to the specified location. Is this expected behavior?
I checked in the responder module that it checking if there are any errors on the model. I inspected the model and it contains validation errors in the #solution object. What am I missing here?
controller:
def create
#problem = Problem.find(params[:problem_id])
#solution = #problem.solutions.build params[:solution]
#solution.save
respond_with(#solution, :location => detail_problem_solution_path(#problem, #solution)
end
model:
validates :body, :presence => true, :unless => :reference
reference is true or false false.
I encountered this problem today, and come upon this Rails issue over at github. The exception seems to be thrown since the route url helper can't generate a valid for unsaved (invalid) records.
There's discussion on the github issue about allowing procs as an argument to the location parameter, but it doesn't look like it'll be added anytime soon.
For now I'll stick with using the following solution:
def create
#post = Post.new(params[:post])
if #post.save
respond_with(#post, location: edit_post_path(#post))
else
respond_with #post
end
end
The only way I was able to solve is this:
def create
#problem = Problem.find(params[:problem_id])
#solution = #problem.solutions.build solution_params
success = #solution.save
respond_with(#solution) do |format|
format.html {redirect_to detail_problem_solution_path(#problem, #solution) } if success
end
end

How do I make a route to a custom controller action in Rails 3?

I'm new to Rails, and a bit confused about routes:
I have a Devices controller:
#devices_controllers.rb
class DevicesController < ApplicationController
def index
#devices = Device.all
end
def show
#device = Device.find(params[:id])
end
def new
#device = Device.new
end
def create
#device = Device.new(params[:device])
if #device.save
flash[:notice] = "Successfully created device."
redirect_to #device
else
render :action => 'new'
end
end
def edit
#device = Device.find(params[:id])
end
def update
#device = Device.find(params[:id])
if #device.update_attributes(params[:device])
flash[:notice] = "Successfully updated device."
redirect_to #device
else
render :action => 'edit'
end
end
def destroy
#device = Device.find(params[:id])
#device.destroy
flash[:notice] = "Successfully destroyed device."
redirect_to devices_url
end
def custom_action
"Success"
end
I'd like to access the "custom_action" action via a url like this:
http://foo.bar/devices/custom_action
I've added this line to my routes.rb file:
match 'devices/custom_action' => 'devices#custom_action'
However, when I try the URL in the browser, I get this error:
ActiveRecord::RecordNotFound in DevicesController#show
Couldn't find Device with ID=custom_action
It seems to be going to #show action instead of #custom_action. If a user id is not supplied, and I go to http://foo.bar/devices/custom_action, I'd like it to go #custom_action.
I've read Rails Routing from the Outside, but still can't still seem to figure out the problem.
I think the problem may be because of the order in which you have defined your routes.
I suspect you have resources :devices in your routes.rb. In addition, I suspect you have defined your custom route after this. If you type rake routes into your console/terminal, you will see that there is already a route defined for the following pattern:
GET /devices/:id
This route is a product of resources :devices, which is taking precedence over your custom route. Referring back to the Edge Guides, specifically in 1.1. Connecting URLs to Code, it states that the request will be dispatched to the first matching route. So a simple fix would be to define your custom route before resources :devices.