Hello.

Stuck on some HABTM stuff here... I'm new to rails and I'm kinda lost
on this topic.
FYI This is all within the context of a working restful_authentication
and role_requirement  setup. I am simply adding a radio button pair to
my new user form. When submitted, I need the appropriate tables
updated. As is, only the user table will update. The join table will
not. I have tried a handful of different things both for the
fields_for params and inside the controller's create action with no
luck yet. Here's what I have below.

Thanks for the input!

THE CLASSES:
class User < ActiveRecord::Base
    has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

THE CONTROLLER: Edited for brevity...
class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    logout_keeping_session!
    username = params[:user][:first_name]
    username  += params[:user][:last_name]
    params[:user][:login] = username

    # Create a random password.
    random_p = User.generate_pass 8
    params[:user][:password] = random_p

    @user = User.new(params[:user])

    success = @user && @user.save
    if success && @user.errors.empty?
      redirect_back_or_default('/')
      flash[:notice] = "Thanks for signing up!"
    else
      flash[:error]  = "Too bad sucka!"
      render :action => 'new'
    end
  end
end


THE FORM: new.html.erb
<% form_for @user do |f| -%>

<p><%= label_tag 'first_name' %><br/>
<%= f.text_field :first_name %></p>

<p><%= label_tag 'last_name' %><br/>
<%= f.text_field :last_name %></p>

<p><%= label_tag 'email' %><br/>
<%= f.text_field :email %></p>

<% fields_for :role, @roles do |ff| %>
  <p>Select account type:<br>
  <%= label_tag :name_user, "User" %>
  <%= ff.radio_button :name, "user" %>

  <%= label_tag :name_admin, "Admin" %>
  <%= ff.radio_button :name, "admin" %>
  </p>
<% end %>

<p><%= submit_tag 'Sign up' %></p>
<% end -%>


Here's a sample of how the params are going out:
Processing UsersController#create (for 127.0.0.1 at 2009-03-16
17:59:38) [POST]
  Parameters: {"user"=>{"first_name"=>"Willy", "last_name"=>"Wonka",
"email"=>"[email protected]"}, "commit"=>"Sign up",
"authenticity_token"=>"05d982e6eabe5eba6dac466723f9402b9522b3b1",
"role"=>{"name"=>"admin"}}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to