I tried everything but cannot make heads or tails of this ruby on rails in ´def new´ methode.
def edge_params
params.require(:edge).permit(:kind, :start_id, :end_id, :property1)
end
This way the parameters end up at the ´Edge.save(edge_params)´ methode but ´:start_id´ and ´:end_id´ are foreign keys that both point to a Node.
def edge_params
params.require(:edge).permit(:kind, :start_id, :end_id, :property1)
p = {
:kind => params[:kind],
:start_id => Node.where("nodeid = ?", params[:start_id]).first,
:end_id => Node.where("nodeid = ?", params[:end_id]).first,
:property1 => params[:property1]
}
end
This executes but it always fails with complaining that all the required (validate presence of kind, start/end_id) are blank. I am doing something wrong here. I need to somehow find the Node ID because otherwise I get the expected Node but got String error. I am new to Ruby and it is quite a lot more difficult than it looks because it does so many things implicitly that one doesn't really know what one is doing. But it needs a ruby hash for the .save methode as much as i could figure out.
I also don't fully understand the require permit part. I got this from the tutorial on the rails page. Would params.require(:edge => [:kind,:start_id,:end_id]).permit(:property1) work too because that would make more logical sense for my data.
Additional Info
class Edge < ActiveRecord::Base
belongs_to :start_id, :class_name => 'Node', :foreign_key => "start_id", :primary_key => "nodeid"
belongs_to :end_id, :class_name => 'Node', :foreign_key => "end_id", :primary_key => "nodeid"
validates :kind, presence: true
validates :start_id, presence: true
validates :end_id, presence: true
end
class Node < ActiveRecord::Base
has_many :start_id, :class_name => 'Edge', :foreign_key => "start_id" , :primary_key => "nodeid"
has_many :end_id, :class_name => 'Edge', :foreign_key => "end_id", :primary_key => "nodeid"
validates :nodeid, presence: true, uniqueness: true, length: { minimum: 2 }
end
<%= form_for @edge do |f| %>
<p>
<%= f.label :kind %><br>
<%= f.text_field :kind %>
</p>
<p>
<%= f.label :start_id %><br>
<%= f.text_field :start_id %>
</p>
<p>
<%= f.label :end_id %><br>
<%= f.text_field :end_id %>
</p>
<p>
<%= f.label :propety1 %><br>
<%= f.text_field :property1 %>
</p>