0

I have two models, Rules and Ruleset that both have a has_to_and_belong_to_many relationship. Rules are individual rules, and Rulesets are a specific collection of rules.

The user's dashboard shows all the rules the user created. I have a button for each rule to "Add Rule to Ruleset". By clicking the button, it should load a select form where the user can select their existing rulesets and hit submit, and voila, its added to the ruleset.

I just don't know how to make this work, as I'm pretty new to rails. If I call the update action on rules, it loads the entire update form, which I don't want. I just want to have the ability for a user to select a ruleset and then add that rule to the ruleset. Here are my models:

class Rule < ActiveRecord::Base
  attr_accessible :description, :user_id, :game_id, :ruleset_id
  has_and_belongs_to_many  :rulesets
  belongs_to :user
  belongs_to :game

  validates :description, presence: true
  validates :user_id, presence: true
  validates :game_id, presence: true

end


class Ruleset < ActiveRecord::Base
  attr_accessible :title, :game_id, :user_id, :rule_id

  validates :game_id, presence: true
  validates :user_id, presence: true
  validates :title, presence: true

  belongs_to :user
  belongs_to :game
  has_and_belongs_to_many :rules

end
tereško
  • 58,060
  • 25
  • 98
  • 150
Ramsay Lanier
  • 426
  • 6
  • 18

1 Answers1

0

You should declare a specific action in the controller for adding rules to rulesets.

When a person selects a ruleset, it will be passed as a parameter and you can catch it in your newly declared action. Also, use a hidden_field_tag where you will store your rule_id.

In the newly declared action, create something like this:

def add_rule_to_ruleset
   @ruleset = Ruleset.find(params[:ruleset_id])
   @rule = Rule.find(params[:rule_id])

   @ruleset.rules << @rule
   redirect_to current_user.rules
end

Also fetch all the rulesets in the select box with current_user.rulesets, but i guess you will have to check if that ruleset has that rule already (you don't want the same rule twice or more times in the same ruleset, do you? ), so you should modify it.

Check that with something like current_user.rulesets.include?(rule)

Routes.rb:

resources :rules do
  member do
    put 'add_rule_to_ruleset'
  end
end
Zippie
  • 6,018
  • 6
  • 31
  • 46
  • Sweet, I'll give this a try. I have the select box working to show only the current user's rulesets, but I didn't think about them adding it twice...good thinking! – Ramsay Lanier Mar 18 '13 at 19:05
  • No problem :) Also you should see to add index to those columns in the intermediate table, so that it never happens that you store two same rows :) Also be sure to catch the errors if something wrong happens (somehow if someone adds same rule twice to a ruleset) Check here about unique indexing: http://stackoverflow.com/questions/4123610/how-to-implement-a-unique-index-on-two-columns-in-rails – Zippie Mar 18 '13 at 19:14
  • 1
    Thanks! Yeah, I've already indexed them. Still trying to get this to work though. I'm so close! – Ramsay Lanier Mar 18 '13 at 19:59
  • I'm trying to figure out how to get the simple form to call the controller action. – Ramsay Lanier Mar 18 '13 at 19:59
  • use a `form_tag` and tell it which controller,action and method to hit. Did you declare the route? – Zippie Mar 18 '13 at 20:04
  • hmmm, no I havent. I think I'm confused about how to name it – Ramsay Lanier Mar 18 '13 at 20:09
  • Im thinking something like this: match 'rules/add_rule_to_ruleset/:id' => 'rules#add_rule_to_ruleset', :via => :put, :as => :add_ruleset – Ramsay Lanier Mar 18 '13 at 20:29
  • But then I get the "No route matches {:controller=>"rules", :action=>"add_rule_to_ruleset"}" – Ramsay Lanier Mar 18 '13 at 20:30
  • In which controller are you going to put in? In the ruleset? – Zippie Mar 18 '13 at 20:31
  • see the last part of the answer, i wrote it there so you can see it more clearly. Now if you run the rake routes you can see it there – Zippie Mar 18 '13 at 20:45
  • Im still getting the error that "No route matches {:action=>"add_rule_to_ruleset", :controller=>"rules"}" I run rake routes and see the add_rule_to_ruleset_rule path, and there is a "add_rule_to_ruleset" action in the rules controller. So confused. – Ramsay Lanier Mar 18 '13 at 21:48
  • Can you post up your form_tag? Did you add `:method => 'put'` – Zippie Mar 18 '13 at 21:54
  • I figured it out. I wasn't calling the path like this: add_rule_to_ruleset_rule_path but it should have been add_rule_to_ruleset_path(rule) – Ramsay Lanier Mar 18 '13 at 21:56
  • great, glad you figured it out and glad i helped you :) – Zippie Mar 18 '13 at 22:00