Is it possible to put validations in controllers in Ruby on Rails? - 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.

Related

Rails - Validating a record without adding errors

Question is as above. I have a record that I need to validate before deciding which form to show. The record is created invalid, and we check its validity to see if the form the user is on is the initial registration form or a subsequent form.
My issue is when I call record.valid? the record gets various error messages added to it which are then incorrectly displayed on the registration form. It's not possible for me to simply clear the errors after validating as I need the errors present if the user enters invalid data.
Is there any way to call valid? or an equivalent that does not add errors to the instance?
Thanks in advance
You want to validates with a steps system ? I think you can look at with_options block.
example :
with_options condition do
# your validations or anything...
# validates :attribute
end
documentation :
https://apidock.com/rails/Object/with_options
With good conditions you will avoid adding errors on validations already "validated" and focus on the new ones.
I hope I understood your request correctly.
After calling valid? you can clear errors from object ie.
record.errors.clear

Rails 4 way of editing multple records individually at once

So I'm trying to use a table to store global settings. I want the /SystemSettings/edit.html.erb to be the only view and controller, which almost works. Unfortunately, I need to edit all records at the same time. I will be using an initializer as I don't want user to create or remove settings, only edit. I'm trying to go by this railscast (#198) but bypass the checkbox way as I also have no index/show views. It seems to be working fine, but when I go to update the form I get undefined method 'keys' for nil:NilClass. You can see all the code on the sys_settings branch of my github repo.
Can it that be that simple that you use :system_settings symbol to access parameters in controller and create form for "settings[]" (and not "system_settings[]" in view?
undefined method 'keys' for nil:NilClass
basically says you don't have any values under params[:system_settings]. Review console messages for the rails server, it prints all incoming parameters for each request. Study what comes for the update request, how parameters are named there.

passing named_scope as argument

I am reusing a controller method and I need to change the scope as required.
I have stored the scope name in a session and would like to be able to do the following.
if params[:scope_name]
session[:submission_scope_name] = params[:scope_name]
else
session[:submission_scope_name] = "allSubs"
end
#search = Submission.session[:submission_scope_name].search do
...
end
The code above is giving me the following error message:
undefined method `session' for #<Class:0x00000002ad7df0>
Is there any way of passing a named_scope as an argument?
You probably don't want to do this from a security standpoint: a malicious user could make a poorly-formed submission_scope that you'll just be sending straight to Submission.
That said, you're looking for the method send here. Try this instead:
Submission.send(session[:submission_scope_name].to_sym).search
send will try to call a method on that object named whatever symbol you passed in. You can read more about it in the Ruby core docs, but ultimately doing that would allow you to send whatever named scopes to Submission you want.

Apply validation module to model in certain controllers only

I have a model that can be edited by two different types of users. The first has a login and has special privileges (let's call them a 'user'). The second is just some random user without a login with limited privileges (let's call them a 'guest').
The guest only really interacts with the model through one controller and we want certain validations to only apply in this case. The validations we want to apply exist within a module.
I tried doing something like this in the controller action, but it didn't seem to work:
#object = Model.find(params[:object_id])
#object.extend SpecialValidations
Then we would check for the objects validity (maybe directly or when updating attributes) and then display any errors generated by the validations.
Is there a better way to do this?
Thanks!
One alternative is to include the following in your Model:
attr_accessor :guest
def run_special_validations?
guest
end
validate :special_validation1, if: run_special_validations?
validate :special_validation2, if: run_special_validations?
Then, by having the controller set #object.guest = true, you will tell the object to run the conditional validations.
You could keep the validation without any conditions, and just skip it in the user controller (by using the update_attribute method, for example).

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.

Resources