views:

375

answers:

3

I'm trying to create a record within a join table from the action of a button. To explain, I would have an events model and would like to track selected events from each user.

I used the HABTM relationship since I dont really need any extra fields.

User.rb => has_to_and_belongs_to_many :events Event.rb => has_to_and_belongs_to_many :users Events_Users Migration => [user_id, event_id, id=>false]

I'm getting stuck on the actually creation of the record.. Someone helped me earlier with adding the record in within the console with worker great.

u = User.find(1)
u.events << Event.find(1) 

Now I would like to perform the action as a result of clicking a link... Is this in the right direction?

   def add
    @user = User.find(session[:user_id])
     @event = Event.find(params[:id])
     if @user.events.save(params[:user][:event])
         flash[:notice] = 'Event was saved.'
     end
end

Should I add a @user.events.new somewhere and if so where do I put the params of which user and which event? Ayuda me por favor.

+1  A: 

The following code should work (assuming that you pass in an parameter with the name id that corresponds to the id of an event object):

   def add
     @user = User.find(session[:user_id])
     @event = Event.find(params[:id])
     @user.events << @event
     flash[:notice] = 'Event was saved.'
   end

The problems I see in your code are:

  1. You are passing a hash to .save. Save should only take a boolean value corresponding whether validations should be run and is true by default. However .create and .new can accept a hash of values. (.save would be used after .new).

  2. You load an event through params[:id] but then you attempt to create an event through params[:user][:event]. Which do you want to do? Create or load? (my example assumes load)

  3. Actions that have an effect such as this one should happen when a user clicks a button and submits a form rather than 'clicking a link'. This code may be vulnerable to cross site request forgery (Someone could trick someone into clicking a link on another site that ran this action). Rails forms, if correctly implemented, are protected against this because they use a request forgery protection token.

  4. Most likely you want to redirect the user after this action. Rendering pages after executing actions like this (rather than redirecting) is considered bad practice.

Gdeglin
do you mean @user.events << @event.name???
ChrisWesAllen
No. That code would not make sense if I am understanding your description of the problem correctly.
Gdeglin
Thanks for the suggestions, I changed the "<%= link_to image_tag("grid_heart.gif", :border=>0 ), :controller => 'event', :action => 'add_event' %>" to "<%= button_to "Add", :controller => 'event', :action => "add" %>" but I still get an error of there being an "uninitialized constant EventController" Did i put the method in the wrong controller?
ChrisWesAllen
That code is not correct, though that's not what the cause of your error is. You use the parameter :controller => 'event', which tells rails to look for an EventController. Rails is telling you that no such controller exists in your project.
Gdeglin
A: 

What you did in the console you need to do in the controller.

def add
  @user = User.find(session[:user_id])
  @event = Event.find(params[:id])
  @user.events << @event
  flash[:notice] = 'Event was saved.'
end

The thing to note here is that the << operator for existing records will cause the association to be persisted immediately.

Take a look at the ActiveRecord documentation for more info.

Randy Simon
A: 

If the event_id is passed as params[:id] and you are adding only one event in this call then, you can do the following in your controller code:

  User.find(session[:user_id]).events << Event.find(params[:id])
  flash[:notice] = 'Event was saved.'

You don't need explicit save to save the has_many association of an existing model instance.

Scenario 1

 u = User.new(..)
 u.events << Event.first
 # Now you need to call `save` in order to save the user object 
 # and the events association
 u.save

Scenario 2

 u = User.first
 u.events << Event.first
 # Don't need to call `save` on `u` OR `u.events`
KandadaBoggu
Im still getting an uninitialized constant EventControllerAny idea where it could coming from?
ChrisWesAllen
Can you post your controller code at Pastie (http://pastie.org/) and provide a link.
KandadaBoggu
I can, but that is the only method I've added to the event_controller. There is no error when I remove the add method, and the rest of the controller was generated from a scaffold so its pretty basic stuff. The current method looks like... def add @user = User.find(session[:user_id]) @event = Event.find(params[:id]) @user.events << @event flash[:notice] = 'Event was saved.' end
ChrisWesAllen
Sorry, I'm not sure how to add code snippets to a comment on stackoverflow
ChrisWesAllen
I think the problem is related to finding the parameter of the event...I;m not sure how to pull that data
ChrisWesAllen