ruby on rails 3 - Refactoring in rails3 -


I have 4 verbs in my controller and I have to check some situations within each action. What is the best way to do this? For example, the condition to be checked for each action is below

  if param [[id] == $ boy.id @rec = $ boy.name elsif params [: id] == $ Girl id @rec = $ girl.name Other render: json = & gt; {"Error" = & gt; The person with "# {Params [: id]} does not exist"} and the end of the return    

You have two options here:

  1. Type a method that checks the conditions and returns the result to call it.
  2. before_filter But if you add more work to your controller, then you have to leave the first_filter for those tasks, which do not need to check the condition. Example: 1:
      DIF check if param [[id] == $ boy.id @rec = $ boy.name elsif params [: id ] == $ girl.id @rec = $ girl.name else @json = {"error" => The person with the <# parameter [: id]} does not exist "} Return @ Jason End & Def some_aunch results = check # Use your result in action #   

    Example 2: < / P>

      before_filter: check, only => gt: [: some_action ,: some_action2] // You can also use it except if you want this check to avoid certain actions # Use some similar Def Definition of DEF, if any AOR / you put the code here @ Json // You end end code here   

    The only difference is that if you follow Phase One, then you have to call the check method to check the conditions. If you use the second option, then you specify the work that will be used on the top before_filter .

    Hope this will be helpful.

Comments

Post a Comment

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -