Where can I turn off paperclips validation with save(false) command - ruby-on-rails-5

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

Related

Rails 3.2.9 is generating a Template Missing error

I am getting the following error in Rails 3.2.9:
Template is missing
Missing template projects/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "c:/Documents and Settings/.../app/views"
I started getting this error yesterday, went back and rebuilt my controller and was still getting the error. I then rebuilt everything with the rails generate scaffold on the command line. When I went in to save a new object instance, I am still getting the same error.
My presumption is that the scaffold generator would produce the correct code on a bare-bones basis, and then allow me to piece by piece, rebuild the functionality.
I am including the code for the model and controller below:
class Project < ActiveRecord::Base
attr_accessible :title, ...
end
++++++++++
class ProjectsController < ApplicationController
# GET /projects
def index
#projects = Project.all
end
# GET /projects/1
def show
#project = Project.find(params[:id])
end
# GET /projects/new
def new
#project = Project.new
end
# GET /projects/1/edit
def edit
#project = Project.find(params[:id])
end
# POST /projects
def create
#project = Project.new(params[:project])
#project.save
end
# PUT /projects/1
def update
#project = Project.find(params[:id])
end
# DELETE /projects/1
def destroy
#project = Project.find(params[:id])
#project.destroy
end
end
Anyone have any ideas? Did Rails develop a bug overnight? I haven't been able to find anything using Google-Fu that might attribute to this.
Thanks.
Not sure about default rails scaffolding in rails but in general you should redirect on create with a success flash message:
#project = Project.new(params[:project])
if #project.save
redirect_to #project, notice: "Success"
else
render :new
end

ActionMailer debugging

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

In Rails 3 how could I create a db only contact form not using scaffold?

New to rails 3
I would like to create a contact form that people fill out, its saved to the db and then a thank you page comes up.
I would like to do this without scaffold so I can learn better, and I figure that by doing it this way it would be easer to setup so that people cannot try and look at other people's entries by modifying the url.
ideally it would keep their state in the session or cookie so that they would end up on the thanks page if they came back.
Have been trying to do this for about 3 days and reading/googling tons, but between the new routes redirect_to controller stuff in rails3 havn't managed to figure it out.
Routes.rb
Contact::Application.routes.draw do
resources :contactees, :only => [:new, :create]
# to make sure crud doesn't have the routest I don't want
get 'contactees/submitted'
root :to => 'contactees#new'
contactees_controller.rb
ContacteesController < ApplicationControler
def share
end
def new
#contactee = Contactee.new
end
def create
#contactee = Contactee.new(params[:contactee])
if #contactee.save
redirect_to submitted_contactee
else
render action: "new"
end
end
end
Views
contactees
_form.html.erb
new.html.erb
submitted.html.erb
Get rid of the submitted route, you don't need it. Perhaps something like this?
def new
render :submitted if session[:contacted]
end
def create
#contactee = Contactee.new(params[:contactee])
if #contactee.save
session[:contacted] = true
render :submitted
else
render action: "new"
end
end

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.