I am having issues permitting all on one of the sub-hashes. How could I do this in strong_params in Rails 4?
params.require(:checkout_item).permit(:checkout_group_transaction_id, :item_type, :property_id,
:image)
params.require(:checkout_item[:data]).permit!
Where :checkout_item[:data] should be permit all ??
What you want to do is whitelist every key in params[:checkout_item][:data], which is a hash. You can use the keys method here to return an array of the keys for that hash.
params.require(:checkout_item).permit(:checkout_group_transaction_id,
:item_type, :property_id, :image,
{:data => params[:checkout_item][:data].try(:keys)})
Now every key in params[:checkout_item][:data] will be permitted regardless of how many keys appear for a given request. Note the use of try to avoid raising a NoMethodError exception on params[:checkout_item][:data] in the event that it is nil since you may not need to pass the :data sub hash on every request to create or update.
I took this idea from the action controller rails guide section 4.5.4 Outside the Scope of Strong Parameters.
Hope this helps!
Related
For example, To access time and name of Calendar events, we can write:
$startTime = $event->Start;
$endTime = $event->End;
$subject = $event->Subject;
But an event's body is not accessible by doing:
$body = $event -> Body
Instead we have to create a separate response and look in the event's extended properties.
Good question. I just did some research on this a week or so ago. Basically, EWS has a concept of First Class Properties and Elements. This means that some properties aren't returned by FindItem and some are, most likely to save space. Items can contain over a hundred properties and their children can contain properties and so on and so forth, so to help keep things simple there are some properties that aren't returned in FindItem and some that are. Here's the quote from the documentation
The set of first-class properties and elements that are returned by the EWS Managed API EmailMessage.Bind method and the EWS GetItem operation is slightly different than the set of first-class properties and elements that is returned by the EWS Managed API ExchangeService.FindItems method and the EWS FindItem operation.
Note that you cannot extend the FindItems method or the FindItem operation to retrieve additional properties and elements such as ToRecipients, CcRecipients, and BccRecipients. If you need to retrieve those values, use the FindItems method or the FindItem operation to get the item IDs of the emails, and then use the Bind method or the GetItem operation, to retrieve the required properties
So basically the answer boils down to "Because you EWS won't let you". If you want to know what properties are First Class and what aren't, there's a good page in the documentation with a table to help determine that, located here.
Finally, might I suggest upgrading the library you're using for your EWS operations? I assume you're using jamesiarmes/php-ews which is the most popular from what I've seen, but I'm maintaining my own fork located at garethp/php-ews which is autoloadable, has a simple access API for some operations and is still under development.
My question concerns a few methods in the Laravel Eloquent ORM. Particularly, 3 methods have caught my attention:
firstOrCreate
firstOrNew
updateOrCreate
I clearly understand what these methods do but did not find a way to determine which operation was performed after calling them.
For example, after calling firstOrCreate, how do I figure out if the model was found in the database or created? I can run a find query first and then create the record if it was not found but that would be very inefficient.
Similarly, how to find out if an object was created or found in the database in case of firstOrNew and if the object was updated or created in case of updateOrCreate.
provided you have the timestamps turned on for those tables you could simply check their values in the returned object.
The "exists" property will return true if the model is backed by the database (false if "new" occurred).
The "wasRecentlyCreated" property will return true if the record was just created (false if it already existed).
This should let you disambiguate between all the cases you mentioned.
I'm building a webapp and I have a model, lets call it "person" and "person" has an attribute "location." I want person to have another attribute, "recentHistory" that is an array with the last, say, 5 locations that the user has had. What is the best way to implement this? I've skimmed the docs and I'm not really sure. The frontend is in AngularJS if that matters (I don't think it should). Is the best implementation to use a beforeUpdate where if they are updating location, it adds it to that array (and pushes out the 3rd index of that array)?
You can either store the data as an array or use associations. If you want to manage the array then the beforeUpdate lifecycle callback will be perfect.
I'm new to AngularJS and I am currently building a webapp using a Django/Tastypie API. This webapp works with posts and an API call (GET) looks like :
{
title: "Bootstrap: wider input field - Stack Overflow",
link: "http://stackoverflow.com/questions/978...",
author: "/v1/users/1/",
resource_uri: "/v1/posts/18/",
}
To request these objects, I created an angular's service which embed resources declared like the following one :
Post: $resource(ConfigurationService.API_URL_RESOURCE + '/v1/posts/:id/')
Everything works like a charm but I would like to solve two problems :
How to properly replace the author field by its value ? In other word, how the request as automatically as possible every reference field ?
How to cache this value to avoid several calls on the same endpoint ?
Again, I'm new to angularJS and I might missed something in my comprehension of the $resource object.
Thanks,
Maxime.
With regard to question one, I know of no trivial, out-of-the-box solution. I suppose you could use custom response transformers to launch subsidiary $resource requests, attaching promises from those requests as properties of the response object (in place of the URLs). Recent versions of the $resource factory allow you to specify such a transformer for $resource instances. You would delegate to the global default response transformer ($httpProvider.defaults.transformResponse) to get your actual JSON, then substitute properties and launch subsidiary requests from there. And remember, when delegating this way, to pass along the first TWO, not ONE, parameters your own response transformer receives when it is called, even though the documentation mentions only one (I learned this the hard way).
There's no way to know when every last promise has been fulfilled, but I presume you won't have any code that will depend on this knowledge (as it is common for your UI to just be bound to bits and pieces of the model object, itself a promise, returned by the original HTTP request).
As for question two, I'm not sure whether you're referring to your main object (in which case $scope should suffice as a means of retaining a reference) or these subsidiary objects that you propose to download as a means of assembling an aggregate on the client side. Presuming the latter, I guess you could do something like maintaining a hash relating URLs to objects in your $scope, say, and have the success functions on your subsidiary $resource requests update this dictionary. Then you could make the response transformer I described above check the hash first to see if it's already got the resource instance desired, getting the $resource from the back end only when such a local copy is absent.
This is all a bunch of work (and round trips) to assemble resources on the client side when it might be much easier just to assemble your aggregate in your application layer and serve it up pre-cooked. REST sets no expectations for data normalization.
Here's my dilemma. I have a series of tables with leads from our various contact forms. We also have tables for spam that comes in on our forms. But occasionally, a lead may get routed incorrectly to a spam table. So I need to move that lead from one table to the other, which means inserting it one and deleting it from the other.
As I understand it, when Backbone calls the save method, it checks to see if that id exists in the table. If it doesn't, it makes a POST request. If it does, it makes a PUT request. I need to be able to force Backbone to make a POST request, so that Laravel can call the right RESTful action.
See the problem is that if Backbone makes a PUT request to say, /send-message/52 (the 52 being the ID of the lead in the send-message-spam table), it will update/overwrite the existing lead with the ID of 52. I want to make a POST request to /send-message (obviously without an ID).
I can force Backbone to use a different urlRoot, but how do I force it to make a POST when I call save()?
I'm unsure if there is a configuration option to do this, but you could always override the save method in your Backbone model to have it perform the action you desire.