ruby on rails - Create and push an association at the same time -


i have word model , category model.

words has_and_belongs_to_many categories. categories has_and_belongs_to_many words.

now, have setup , running in console, example can do:

word.create(title: "testicles") testicles = word.first  category.create(title: "genitalia") genitalia = category.first  testicles.categories << genitalia  testicles.categories => "genitalia" 

now can , running using forms in views too, if have separately created category in own form on separate page, , same word. in word show view can create form assign category it.

however... want do @ same time when create word i.e. on 'new word' view.

i'm having big problems working out how this. think i'm right in saying can have 1 form , 1 submit in view, think somehow have send form to, say, wordscontroller, , work magic in there, here giving me big headaches. can help?

i haven't created user model or setup authentication yet, there no obstacles in respect.

models/word.rb:

class word < activerecord::base   belongs_to :user    has_and_belongs_to_many :categories    validates :title, presence: true end 

models/category.rb:

class category < activerecord::base     has_and_belongs_to_many :words      validates :title, presence: true end 

schema.rb:

activerecord::schema.define(version: 20150529144121)    create_table "categories", force: :cascade |t|     t.string   "title"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    create_table "categories_words", id: false, force: :cascade |t|     t.integer "category_id"     t.integer "word_id"   end    add_index "categories_words", ["category_id"], name: "index_categories_words_on_category_id"   add_index "categories_words", ["word_id"], name: "index_categories_words_on_word_id"    create_table "words", force: :cascade |t|     t.string   "title"     t.text     "description"     t.integer  "user_id"     t.datetime "created_at",  null: false     t.datetime "updated_at",  null: false   end    add_index "words", ["user_id"], name: "index_words_on_user_id"  end 

after experimenting various form_tag wizardry (and failing badly), @ moment i'm using form:

<%= form_for(@word) |f| %>   <% if @word.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@word.errors.count, "error") %> prohibited word being saved:</h2>        <ul>       <% @word.errors.full_messages.each |message| %>         <li><%= message %></li>       <% end %>       </ul>     </div>   <% end %>    <div class="field">     <%= f.label :title, 'word' %><br>     <%= f.text_field :title %>   </div>   <div class="field">     <%= f.label :description, 'definition' %><br>     <%= f.text_area :description %>   </div>   <div class="field">     <%= f.label :categories, 'category' %><br>     <%= f.text_field :categories %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

in view, form has:

#<category::activerecord_associations_collectionproxy:0x007f16dd834820> 

in 'category' box, can cleared , own category inputted. when submitting form fields filled in, nomethoderror.

so want create both word , category @ same time, , link them. if classical solutions (for example using nested_form gem) don't want, can try approach.

you want custom view/controller , business in controller suggest. suggest instead used simple form_tag can add fieldsets.

view

<%= form_tag your_custom_route_path, :html => {:class => "form-horizontal"} |form| %>      <%= fields_for :word, @word |f| %>       <%= f.text_field :title %>     <% end %>      <%= fields_for :category, @category |f| %>       <%= f.text_field :title %>     <% end %> <% end %> 

routes

post '/yourroute', to: 'your_controller#your_action_create', as: 'your_custom_route' 

controller featuring actions

class yourcontroller < applicationcontroller    # new   def your_action_new     @word = word.new     @category = category.new   end    # post   def your_action_create     @word = word.new(word_params)     @category = category.new(category_params)     if @word.save , @category.save       @word.categories << @category       @word.save      end   end    private    def words_params     params.require(:word).permit(:title, ...)   end    def category_params     params.require(:category).permit(:title...)   end 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -