Passing user_id, site_id, and question_id to same table on create...

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-05-20T06:08:58Z Indexed on 2010/05/20 7:10 UTC
Read the original article Hit count: 166

I can't seem to figure out how to do this. I am trying to pass four different variables to a single table. To be more specific I am trying to create a "like" in a likes table that also captures the site_id (like an answer), user_id, and the question_id. Here is the set up.

class Like < ActiveRecord::Base
  belongs_to :site
  belongs_to :user
  belongs_to :question
end

I will spare you the reverse, has_many associations but they are there. Here is the likes controller where I think the problem is.

class LikesController < ApplicationController

  def create
    @user = current_user
    @site = Site.find(params[:site_id])
    @like = @site.likes.create!(params[:like])
    @like.user = current_user
    @like.save

    respond_to do |format|
     format.html { redirect_to @site}
     format.js
     end
  end
end

This code successfully passes the like and site_id but after many different variations of trying I can't get it to pass the question id. Here is my form:

/views/sites/_site.html.erb (though the partial is being displayed in the /views/questions/show.html.erb file).

<% remote_form_for [site, Like.new] do |f| %>
        <%= f.hidden_field :site_name, :value => "#{site.name}" %>

        <%= f.hidden_field :ip_address, :value => "#{request.remote_ip}" %>
        <%= f.hidden_field :like, :value => "1" %>
        <%= submit_tag "^" , :class => 'voteup' %>
    <% end %>

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby