Calling a method from controller - ruby-on-rails

I have a method to generate a password in models in Users.rb file. In my view i have a form where only email address filed is there and I want to to add the password to the User attribute before saving it. I have a create method where I create a new user and save it in controller. How do I call the generate password method in the controller?

Is it a class method? Call it using User.generate_password. Is it an instance method? Call it using #user.generate_password, or whatever the instance variable is named.

Related

Rails4 Access controller_name from model method

I have user role based access control. User has many roles.
Each role has access to some controllers, actions and scopes.
I have method can_control?(controller) in User model which check if user have access to specific controller. I have similar method to actions.
Then in view or controller I can make simple logic to hide some information or permit access using:
current_user.can_control?(controller_name)
I wonder if it possible to create method in User model which automatically takes controller_name. I tried to define method in model.
def can_control?
self.permitted_cotrollers.include?(controller_name)
end
But it gives me an error:
undefined local variable or method `controller_name' for #<User:0x007f00e8ceb928>
I understand error, but can find solution or if it possible to have one.
Here is the best solution I can think of : You can access the current controller name in params[:controller] from any controller method.
In User model :
def can_control?(params)
self.permitted_controllers.include?(params[:controller])
end
In any controller :
current_user.can_control?(params)
In your model, you can get the standard associated controller name by using :
"#{self.class.to_s}Controller"
self refers to your model
.class gets its class
.to_s converts its class to a string containing its class name
If you need it written in snake_case instead of CamelCase, use this :
"#{self.class.to_s.tableize}_controller"
.tableize converts the CamelCase name of your model into the snake_case name used in your controller

Getting params from URL in model's create method

Is there a way to get the params from URL in the create method?
My URL for the "new" view is:
/model_name/new?other_model_id=100
I would like to be able to alter the model with ID 100 when I create a new model. I've tried calling params[:other_model_id] in my "create" method, which returned nil and I tried setting the following variable in my "new" method:
#other_model = Model.find(params[:other_model_id])
I have a field called "other_versions" in my model, which is an array of model IDs. When I create a new model I want to add the new model's ID to the array of model IDs in the old model.
Why don't you use the after_createfilter (http://guides.rubyonrails.org/active_record_callbacks.html) and just add the "other_model" id on an hidden field on your create/edit form?
Please make sure, if you are using Rails >4, to add that parameter on your strong parameters (http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html) otherwise it will always be empty when you check on params.

ASP.NET MVC - where should my function put? Controller? Model?

For example, I have a password generator in my user registration website. Where should I put the function of generating password?
Put together with UserController?
What is the correct way to put these functions?
I would recommend putting it into a class of its own. For the sake of SRP, your UserModel should do things with a User and only a User. Your UserModel class should not be responsible for generating passwords for new users. Separate it into its own class and call a method on that class during the creation of your new user in your UserModel.
I would put it in my User Model.
Or you could create a Utility class and put it in there.

Passing a URL to code in a helper method.

I created a Foo controller, and the Foo view allows users to enter and submit a URL. In my Foo helper I have a block of code which scrapes the URL entered by the user (Using nokogiri). How do I pass the url received from the user to the helper so that URL can be parsed and saved to the db? Should I set this up differently?
One way to archive this in Rails 3 is to call view_context inside the
controller to create a new ActionView instance for a controller, then
all helper methods will be available through this instance in the
controller.
view_context.scrape_url_method_in_helper
Or do this in FooController
include FooHelper
Also, read rik.vanmechelen's comment below.

Calling an action in another controller

I have a situation like when i submit the employee form i need to check for a boolean value in employee_controller if it is set then i need to invoke a create action in AddressController. what is the best way to do it ? Whether i need to create an instance like AddressController .new and call the method ? and there is no relationship between employee and address.
Why do you need to call an action in AddressController when you submit and Employee form?
A better/cleaner idea might be to interact with the Address model from within the Employees_controller. Nothing wrong with that!

Resources