Hello All
I am unsure how to handle the following in terms of the “Rails way”:
The design requirement: A user may create a “goal” for themselves or (if
they have sufficient permissions) they may create a goal for somebody
else.
Once the user has filled in the form and clicked “submit”, I am unsure
where I should put the logic to check that the User may create the goal?
Do I put the logic in the controller that processes the form data?
Or, must it go into the Goal model ?
Ordinarily, I would have put that in the controller, but most of my
reading suggests that all business logic should reside in the model.
Any guidance would be appreciated.
Regards
Rory
Visit Indonesia 2008 wrote:
Once the user has filled in the form and clicked “submit”, I am unsure
where I should put the logic to check that the User may create the goal?
Do I put the logic in the controller that processes the form data?
Or, must it go into the Goal model ?
> in Model you can put many validations of data like :
>
> class UserGoal::ActiveRecord < Base
>
> validate_presences_of :name, :message => "is already used"
> validates_numericality_of :goal, :message => "must be in number"
> #end more validations
Thanks Reinhart
Unfortunately, the validation in this case is a bit more complicated
than the standard ones:
If I have sufficent permissions, I may create a goal for another user,
if not I may only create a goal for myself.
Therefore, before I can create the goal, I need to check the logged-in
user’s permissions and if these are insufficient, I can only allow the
user to create a goal if it is being created for him/herself. Would this
still go into the model - perhaps as a class method?
Thanks
Rory
Therefore, before I can create the goal, I need to check the logged-in
user’s permissions and if these are insufficient, I can only allow the
user to create a goal if it is being created for him/herself. Would this
still go into the model - perhaps as a class method?
in this case you have to create business logic in controller not model.
Model is only control or validate input or output data that
corresponding to your form. to check permission you can write code in
controller. example :
in Controller
get_permission = Friend.permission_from_friend
if get_permission
put your code here to manipulate friend goal and yours.
else
put your code here to manipulate your own goal.
end
Maybe you can share your code here to find best solution of your
problem. You can use http://pastie.caboo.se to paste your code and paste
the link here after save it.
From:
Reinhart
http://teapoci.blogspot.com
Once the user has filled in the form and clicked “submit”, I am unsure
where I should put the logic to check that the User may create the goal?
Do I put the logic in the controller that processes the form data?
Or, must it go into the Goal model ?
The goal of form will be stored in database, any activities of database
is configured in model, controller is only for calling model to process
input data from form.
in controller :
def create
noob_goal = UserGoal.new(params[:noob_goal])
#to ensure your input data is passing validations
#in model before store in database, you can use it:
if noob_goal.save
flash[:notice] = “Good, your data is processed”
else
end
end
to display error message you can put in view this command :
<%= errors_message ‘noob_goal’ %>
in Model you can put many validations of data like :
class UserGoal::ActiveRecord < Base
validate_presences_of :name, :message => “is already used”
validates_numericality_of :goal, :message => “must be in number”
#end more validations
end
Enjoy and Hope it’s usefull
Reinhart
http://teapoci.blogspot.com
Visit Indonesia 2008 wrote:
corresponding to your form. to check permission you can write code in
put your code here to manipulate your own goal.
end
Thanks Reinhert
That has answered my question. I will perhaps paste some code, later on,
when I have something worth pasteing - at the moment, I am really just
stubbing.
Rory