Passing Values to Controllers

Posted by Dru on Stack Overflow See other posts from Stack Overflow or by Dru
Published on 2011-11-24T16:57:21Z Indexed on 2011/11/24 17:51 UTC
Read the original article Hit count: 250

I'm trying to allow users to 'favorite' links (that is, create a new Favorite record with their user_id and the link_id) This is what I have so far..

When I click favorite (as a user), the new record is assigned to the user_id but the link_id field is nil. How can I pass the link_id into my FavoritesController?

My View Code

Added Link Model Code

class FavoritesController < ApplicationController
  def create
    @user = User.find(session[:user_id])
    @favorite = @user.favorites.create :link_id => params[:id]
    redirect_to :back
  end
end 

The Favorite model belongs to :user and :link

Note: I've also tried this but when I click 'favorite', there's an error "Couldn't find Link without an ID."

Update

<%= link_to "Favorite", :controller => :favorites, :action => :create, :link_id => link.id %> 

with

class FavoritesController < ApplicationController
  def create
    @user = User.find(session[:user_id])
    @favorite = @user.favorites.create :link_id => :params[:link_id]
    redirect_to :back
  end
end

Returns "can't convert Symbol into Integer"

app/controllers/favorites_controller.rb:4:in []
app/controllers/favorites_controller.rb:4:in create

I've tried forcing it into an Integer several ways with .to_i

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby