views:

38

answers:

2

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 %>
A: 
<%= f.hidden_field :question_id, :value => params[:id] %> 

or

<%= f.hidden_field :question_id, :value => @question.id %> 

Try this line inside your form, as you said you are in show page of questions, the params[:id] will be having the id of the question.

If I misunderstood your question, please let me know.

Vamsi
Awesome man. Thanks.
bgadoci
You are welcome :).
Vamsi
A: 

You don't seem have an input for your question_id in your remote_form_for. Is that correct?

If so, you need to either pass your current question to your remote_form_for, or explicitly include a hidden field storing your question_id.

For example:

<% remote_form_for [@question, site, Like.new] do |f| %>
nfm