Hello all. I am hoping someone can clear up a problem I am having.
Here is the background:
a) I am using REST (create my scaffolds with scaffold_resource)
b) I have installed the RESTful_Authentication plugin
c) I have created a model called user (through the RESTFul
Authentication plugin) and a model called group. The group model is for
storing information on groups that are centered around a topic.
I now want a database table that will store the users and the groups to
which they join. So, I created a join table called groups_users using
generate model and migrating the data. This is the correct thinking
right? Then I set up my associations with has_many in my user and group
model.
Interesting note is in my mySQL db it creates the db as group_users and
not groups_users. Convention in Rails says to use groups_users.
Changing my mySQL db table name to groups_users will not work and throw
an error in my rails application. Only way to get it to work was using
group_users. Not sure why that happened.
The actual interaction with the application works like this:
The user creates a group (works fine, no errors in my code, populates
db). Then they can see the groups (show in my controller, works fine,
no errors in code). I have a button on my page that says "join
group."
I have 2 hidden fields that pass the user name (they must be logged in)
and the group name (value is populated from show action). Clicking on
the join button displays no errors or problems and redirects to the
correct view (create.rhtml) but doesn''t populate the db with the
group_id, user_id, group name and username information. Here is my
code:
group_users db migration information:
t.column :group_id, :integer
t.column :user_id, :integer
t.column :groupname, :string
t.column :username, :string
my form on the show.rhtml view:
<% form_for :GroupUser,
:url=>{:controller=>''groups_users'',
:action=>''create''} do |f| -%>
<%= f.hidden_field :groupname, :value=> @group.name %>
<%= f.hidden_field :username, :value=> current_user.login %>
<p><%= submit_tag "Join this group" %></p>
<% end -%>
I had to use :url=>{:controller=>''groups_users'',
:action=>''create''
because I couldn''t get it to work any other way. I don''t
think that is
right for REST.
My GroupsUsers controller:
def create
@GroupUser = GroupUser.new(:groupname=>params[:groupname], :username
=> params[:username])
@GroupUser.save
end
I have researched this in my Agile Web Development with Rails book and
understand associations, but it doesn''t talk about using forms and
associations. Also have peepcode screencasts and Josh Susser''s join
table blog post. While these help with associations, it doesn''t talk
about my scenario or get me to a point where I can understand.... Sorry
for such a long post, but not sure where to go from here.
thanks.
Mike Riley
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---