javascript - Rails - Saving a model with has_many :through association from AngularJS layer -


I am making an angular JS app with the rail at the back end I tags Strong> note , but I can not understand it. I am quite convinced that the way my data is presented in POST request, there is something to do with it, Looks like:

  Post for "127.0.0.1" / "Initial Note / Note" 2014-04-29 09:53:04 +1000 Notes by NotesNotes :: NotesController # Create as HTML Parameter: "Body" = & gt; "Hello", "note_type_id" => 2, "tag_id" = & gt; [1, 3], "note" => {"Body" = & gt; "Hello", "note_type_id" => 2}}  

Here is the note model in racing :

  square notes  

Here my Notes in Rails :

Class Notes Controller & lt; ApplicationController def can be made @ Note = Note. New Note_Permiss if @Note SAVAS present json: @Note, Status: 201 Other render JS: {Errors: @note. Errors}, Position: 422 End and Private DIP Note Parameters. Note: .Permit (: body,: note_type_id,: tag_ids) and end

in the form I have a list of tags filtered by an input when you If you click on a tag in the filtered list, then it adds a tag to the targetNote colliler model:

  & lt; Form name = "noteForm" ng-submit = "process note (notify. $ Valid)" Novellidate & gt; & Lt; Ul class = "list-inline" & gt; & Lt; Li ng-repeat = "in targetNote.tags" & gt; {{T.name}} & lt; / Li & gt; & Lt; / Ul & gt; & Lt; Input id = "ad-tag" type = "text" ng-model = "tag"> & Lt; / Input & gt; & Lt; Ul & gt; & Lt; Li ng-repeat = "tags in tags: filter: tagignation" & gt; & Lt; Button type = "button" class = "btn btn-default btn-sm" ng-click = "adtag (t)" gt; {{T.name}} & lt; / Button & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; Button type = "submit" class = "btn btn-primary" ng-disabled = "notform. $ Invalid" & gt; {{FormAction}} & lt; / Button & gt; & Lt; / Form & gt; These are the relevant methods in my  angular control :  
  TextNotesContributor ("notcity", ["$ scope", " Note "," tag "," alert ", function ($ scope, note, tag, warning) {$ scope.targetNote = new note (); $ scope.tags = Tag.query (); $ diameter process notes = work (IsValid) {$ scope.targetNote. $ Save (function (n, feedback header) {Alert.add ("success", "note updated successfully!", 5000);}, function (n, feedback header) { Alert .add ("Warning", "Error saving note!", 5000);});}; $ scope.addTag = function (tag) {if ($ scope.targetNote.tags.indexOf (tag) & lt; 0) {$ scope.targetNote.tags.push (tag); if ($ ("Tag_ids" in $ Scope.targetNote)) {$ scope.targetNote ['tag_ids'] = [];} $ scope.targetNote. Tag_ids.push (tag.id);}};}]);  

My last answer was work, but in the end I had finished something different . / P>

At the end of the tracks on my notes model I have defined this setter:

  def tag_list = (names) self.tags = names.map do | N | :: tag. Where (name: en) First_or_Carate! Finally end  

In this way I was able to send an array of tag names without worrying if the tag was already created or not.

In the controller I have defined my strong parameters such as

  def noteparam Urine ((body:, note_type_id, {tag_list: []})  

note : My model is part of a rail engine, but the tag model parents This is the reason that I am referring to the model as :: tag . Generally, just the tag is quite good.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -