www.cheapsportsnbajerseysonline.com
in camera talk Wed Dec 25, 2019 3:45 amby akobaxaz • 230 Posts
In Rails3 Cheap Jordan 32 , we use attr_accessible or attr_protected(Protected Attributes) to white-list attributes of a model for mass assignment.
In Rails4, Protected Attributes was moved out as a Gem and similar feature was implemented at the controller level, which is now known as Strong Parameters.
We were migrating one of our project from Rails3 to Rails4 and decided to use Rails’ Strong Parameter instead of using the Protected Attributes Gem. While migrating from Protected Attributes to Strong Parameters we found ourself in a situation where we were repeating the same code. To elaborate, in Rails3 when we used Protected Attribute Cheap Jordan 3 , all our white-listed attributes were at one place i.e. in the model itself. But when we used Strong Parameters, all these attributes of model came into the controllers, so if we had more than one controller dealing with same model, we were repeating the code for white-listing attributes in each controller.
For example:
We had controllers like OrdersController Cheap Jordan 28 , Admin::OrdersController, Api::OrdersController etc. for the same resource, lets say Order. All these controllers were dealing with Order model, so we needed to white-list order’s attributes in these three controllers by something similar to:
# libcontrollersorders_
class OrdersController < ApplicationController
def order_params
ire(:order).permit(:parameter1 Cheap Jordan 2 , :parameter2)
end
end
# libcontrollersadminorders_
class Admin::OrdersController < ApplicationController
def order_params
ire(:order).permit(:parameter1, :parameter2)
end
end
# libcontrollersapiorders_
class Api::OrdersController < ApplicationController
def order_params
ire(:order).permit(:parameter1, :parameter2)
end
end
The pain point was that whenever there was a new column added or removed in Order model, we had to modify order_params in all these three controllers Cheap Jordan 19 , as:
# libcontrollersorders_
class OrdersController < ApplicationController
def order_params
ire(:order).permit(:parameter1, :parameter2, :parameter3)
end
end
# libcontrollersadminorders_
class Admin::OrdersController < ApplicationController
def order_params
ire(:order).permit(:parameter1, :parameter2 Cheap Jordan 18 , :parameter3)
end
end
# libcontrollersapiorders_
class Api::OrdersController < ApplicationController
def order_params
ire(:order).permit(:parameter1, :parameter2, :parameter3)
end
end
In order to manage white-listed attributes in a better way, we wanted to have a single place where we can manage all the white-listed attributes of all models instead of checking and updating every related controller. While discussing this problem with our team members Cheap Jordan 17+ , someone suggested to give a look at the way Spree has wrapped Strong Parameters into a module.
To leverage spree’s approach in a non-spree project:
We created a module, PermittedParameters, in libmodules and created class variables as an array of permitted parameters of the resource. Something like this:
# libmodulespermitted_
module PermittedParameters
mattr_accessor :order_parameters
@@order_parameters = [:parameter1, :parameter2]
end
Now we could use our PermittedParameters module in controllers as:
# libcontrollersorders_
class OrdersController < ApplicationController
def order_params
ire(:order).permit( r_parameters)
end
end
# libcontrollersadminorders_
class Admin::OrdersController < ApplicationController
def order_params
ire(:order).permit( r_parameters)
end
end
# libcontrollersapiorders_
class Api::OrdersController < ApplicationController
def order_params
ire(:order).permit( r_parameters)
end
end
And to add or remove any permitted parameter Cheap Jordan 15 , we just need to modify PermittedParameters Module.
# libmodulespermitted_
module PermittedParameters
mattr_accessor :order_parameters
@@order_parameters = [:parameter1, :parameter2, :parameter3]
end
However, there can be some cases where we need to permit few attributes of associated model too. For example Cheap Jordan 14 , to permit line_items attributes with order, we can define a StrongParameterHelpers module and create methods which will merge line_item_parameters in order_parameters and then we can use these methods in our controller.
# libmodulesstrong_parameter_
module StrongParameterHelpers
def permitted_order_parameters
r_parameters + [line_items_attributes: _item_parameters]
end
end
We can use the above module in the controllers as:
# libcontrollersorders_
class OrdersController < ApplicationController
include StrongParameterHelpers
def order_params
ire(:order).permit(permitted_order_parameters)
end
end
# libcontrollersadminorders_
class Admin::OrdersController < ApplicationController
include StrongParameterHelpers
def order_params
ire(:order).permit(permitted_order_parameters)
end
end
# libcontrollersapiorders_
class Api::OrdersController < ApplicationController
include StrongParameterHelpers
def order_params
ire(:order).permit(permitted_order_parameters)
end
end
This way we can have all the attributes at one place which are easy to manage. Kindly let us know your thoughts on this approach, or if there is another better way to handle such cases.
Source –
---
Keywords: Spree Commerce Extension, Spree Commerce Themes Cheap Jordan 13 , Ruby on Rails Consulting
By: Vinsol
Arti
When an. Cheap Soccer Jerseys Free Shipping Cheap NHL Jerseys Wholesale Cheap Kids NBA Jerseys Cheap MLB Jerseys Online Wholesale Jerseys Cheap Adidas NHL Jerseys From China Cheap Authentic NFL Jerseys Cheap Nike NFL Jerseys Online Cheap College Jerseys Online Cheap Nike NBA Jerseys China
|
Board Statistics
The forum has 4204
topics
and
4207
posts.
|
Einfach ein eigenes Xobor Forum erstellen |