I have a function to take ownership of a job which updates the database to update the username in a table row. I want to link to this function from the view and then redirect to the appropriate page.
How do you link to a controller function or a model function from the view?
from the index i want to have another link beside show, edit, delete, which says 'take ownership'
This will then fire off an action in the application controller
def accept_job(job_type, id, username)
if (job_type == 'decom')
Decommission.update(id, :username => username)
else
end
end
You can use the instance variable #controller to get a reference to the controller. As for calling a model function, you can call Model.function to call class methods, or if you have a particular Model instance called model_instance, then use model_instance.function to call an instance method.
Edit: Okay, I think I understand what you're asking now.
You should
Create a new action in the controller, let's call it update_username:
def update_username
job = Job.find(params[:id])
job.your_method #call your method on the model to update the username
redirect_to :back #or whatever you'd like it to redirect to
end
Add your action the routes in routes.rb. See Rails Routing from the Outside In for more details.
Add your link in the view:
<%=link_to "Update my username please!", update_username_job_path%>
First you create a function in your model, say
class Decommission
def assign_permission(name)
#your update code
end
end
As I can see, you can do this in 3 different ways
1 - Create a helper method to update the permission (This can be done either in Application helper or helper related to your view)
2 - By creating a controller method (as you proposed) But if you are not using this method in other views you dont need to create this method in application controller
3 - If you want to use your method in both controllers and views, create your method in application controller and make it as helper method. By that way you can access it from controllers as well as views
cheers
sameera
Related
I have an application in which user can add a question, so user will fill the modal form, I have two model Client_prefernces and questionnaire
questionnaires table has set of the questions, add question modal form is in the client_preferences view template,
Questionnaire controller create method
def create
#question = Questionnaire.new(clientPreference_params)
#question.save
redirect_to(saveJson_client_preferences_path)
end
private
def clientPreference_params
params.permit(:addedLabel, :addedQuestion)
end
params are
Parameters: {"data_curated_id"=>"", "addedLabel"=>"style", "addedQuestion"=>"whats your fashion type"}
how to capture that params in questionnaire controller create an action to create an entry in questionnaires table and then redirect to saveJson_client_preferences_path(which is a get action in client_preference controller to display questions)
I am getting this error
undefined method `addedLabel=' for #
Thanks in advance
You need to change you strong parameter code to
def clientPreference_params
params.require(:questionnaire).permit(:addedLabel, :addedQuestion)
end
Hope it works.
Try:
params.require(:questionnaire).permit(yourModelname_1: params[:addedLabel], youModelname_2: params[:addedQuestion])
As it allows you to interact with parameters attributes available from a request you implemented.
I was wondering if anyone knows a simple way to make devise 'create' an account if it doesnt exist at login, allowing me to use a single page.
Yes, this is possible. Devise's sessions#create method yields to a block - see https://github.com/plataformatec/devise/blob/master/app%2Fcontrollers%2Fdevise%2Fsessions_controller.rb#L20
So, you'd need to override that controller's create method, and use the yield to add a new account if the user is not successfully signed in.
Edit: here's a quick pseudo-code example of what your overriden create method should look like:
def create
super do |user|
return if current_user # successful login
# Here, if the user doesn't exist,
# create a new record and log them in.
end
end
You need to custom your controller and put the 'create' action in it.
Here's the guide for customizing your controller: https://github.com/plataformatec/devise#configuring-controllers
Rails 3.2.18
Ruby 2.1.5
In my controller, I have new and create methods. I have another method in the controller, let's call it post_create that I would like to automatically trigger after the create method finishes executing. This method has no view associated with it, and it's supposed to update a table.
I tried doing
after_filter :post_create, only:[:create]
But I need the parameters that were passed to the create method, and at this point I no longer have them. Any ideas?
It can be done in this way:-
def create
#code
redirect_to post_create_path(parameter)
end
def post_create
#update the table
redirect_to desired_path
end
Note:- Set the path by knowing the routes by typing in the console as rake routes.
You will get params in you after_action. If this is not the case. You can perform such action in observer. If this is something that does not affect your new view. Then you should go for observer instead of creating a after_action. For mor detail check here.
My Rails app has events and users. In ActiveAdmin, an event can be edited via the form action. If the edit includes attaching a user to the event, I need to send a message to that user. I think I need to either customize the update action or trigger the message-send in an :after_update callback in my event model.
I guess it makes more sense to add a callback, but I'm curious whether it's possible to customize the update action in ActiveAdmin. Is it?
You can edit ActiveAdmin controller actions, but if the action you do after update is the same when updating form outside admin panel than it's better to use callbacks I guess. Why writing more code?
http://activeadmin.info/docs/8-custom-actions.html#modify_the_controller
ActiveAdmin.register Post do
controller do
# This code is evaluated within the controller class
def define_a_method
# Instance method
end
end
end
It's definitely possible, in your ActiveAdmin model just add :
controller do
def update_resource(object, attributes)
attributes.first[:your_attribute] = ...
object.send(:update_attributes, *attributes)
end
Hope it helps!
Is there a way to check which controller method was called from within the model?
Example:
Say the controller create method was called:
def create
do something
end
Then in the model do something only when create in the controller was called
if create?
do something
end
I'd imagine you could examine the call stack but this is exactly what models are not for: they should now nothing about the controller.
Examining the stack:
if caller.grep /create/
# do something
elsif caller.grep /update/
#do something else
end
Should do the trick.
Just pass a create flag to the model method, or make two different methods in the model and call the appropriate one from the controller. Otherwise you are creating a rather unpleasant dependency between the controller and the model. As you noted, validation methods take a parameter to specify when they are run.
Check
if params[:action] == 'create'
Inside your model you can ask/know if the record you are handling is a new record or not
p = Post.new
p.new_record? => true
p = Post.first
p.new_record? => false
maybe that helps you enough?
Otherwise inside a model you can add callbacks, e.g. a before_create that is only called before a new record is saved. To keep your model lean, and you should have a lot of callbacks, those could be grouped inside an observer.
Hope this helps.