Model has 2 foreign keys. Cant insert foreign key into the table of an other table. mysql database. ruby on rails app -
i having problems rails application. have 3 tables( skill, version, , skillversion). skillversion has 2 foreign key columns skill , version) have form skillversion , every time add skill associated version, should add skill , use version drop down list(gets list version table).
issue: when submit form. adding new version row in version table different id. dont want add different row if similar version exists. after submitting skillversion table, should id of existing version , add version_id column in skillversion. please , let me know if need more information. attaching relevant code project.
right now: doesnt add value skillversion table
model/skill.rb
class skill < activerecord::base has_many :skillversions end
model/skilversion.rb
class skillversion < activerecord::base belongs_to :skill belongs_to :version end
model/version.rb
class version < activerecord::base has_many :skillversions end
controllers/skillversions_controller.rb
def create @version = version.find(:number) @skill = skill.new(params[:skill].permit(:name)) if @skill.save @skillversion = skillversion.new(params[:skillversion].permit(:version_id, :isactive)) @skillversion.skill = @skill @skillversion.version = @version if @skillversion.save redirect_to skillversions_index_path else render json: @skillversion.errors, status: :internal_server_error end end render json: @skill.errors, status: :internal_server_error end
views/skillversions/_form.html.erb
<%= fields_for :skill |s| %> <%= fields_for :version |v| %> <form class="form-horizontal" id="dform"> <div class="control-group"> <%= s.label :name, 'skill name', :class => "control-label" %> <div class="controls"> <%= s.text_field :name, :class => "controls" %> </div> </div> <div class="control-group"> <%= v.label :number, 'version', :class => "control-label" %> <div class="controls" > <%= v.collection_select :number, version.find(:all), :id, :number, :class => "dropdownwidth" %> </div> </div> <div class="control-group"> <div class="controls"> <label class="checkbox" > <%= sv.check_box :isactive, :class => "controls " %> <%= sv.label :isactive, 'active?', :class => "checkbox" %> </label> <%= sv.submit 'create it!', :class => "metro" %> </div> </div> </form> <% end %> <% end %>
thank in advance!
Comments
Post a Comment