问题
I'm using neo4j in my current app and it is my first time with this database, then I have a lot of doubts. The most important now is the following.
I'm customizing my input forms (as you may see in this question I asked) and here is one of my models:
class Contact
include Neo4j::ActiveNode
property :first_name, type: String
property :surname, type: String
property :email, type: String
property :phones, type: String
has_one :in, :address, type: :HAS_ADDRESS
has_one :in, :title, type: :HAS_TITLE
has_one :in, :gender, type: :HAS_GENDER
end
As I understand things, these relations create their own properties in my model, because when I generate my scaffold the properties :address, :title and :gender are all in the form, even if they are not explicitly created in my model.
It happens that some of these properties should not be exhibited. This is the case of :address, which I'll render in a partial in the appropriate situations.
The other properties defined by neo4j relations by default , :gender and :title, should be exhibited as HTML selects.
Here is my question: How may I distinguish, when creating my form programmatically, among the properties I described and those generated by the relations at form generator level?
In other words, what should I put in my **lib/templates/erb/scaffold/_form.html.erb to guarantee that some of these properties are not exhibited and others are exhibited as HTML select tags?**
I though about creating some kind of methods like is_displayable? and is_select? in the property, but I really don't know where I may do so, because these properties are not explicitly declared anywhere.
Any suggestions about this?
回答1:
I wouldn't say that the associations create properties. Associations (has_one
and has_many
) and properties create methods on your Ruby model class and it's objects.
That aside, you could do this in a number of ways, none of which are really specific to the neo4j
gem (nor would it be specific to ActiveRecord
if you were doing that).
You could have an array in your view which you iterate over. You could have a constant in the model which defines which are editable and then define a is_editable?
similarly to what you suggest.
But probably neither of those would be recommended in the model-view-controller setup in Rails. Controllers are generally responsible for enforcing strong parameters, so you'd probably want a constant in there which can be used both as a variable passed down to the view as well as using it to verify values as they are being submitted (because just because your view doesn't show those fields, it doesn't mean a malicious party couldn't create their own form or submit with something like the Postman plugin for Chrome)
来源:https://stackoverflow.com/questions/35947181/dont-want-to-display-neo4j-relations-in-a-form