lundi 27 juillet 2015

Fetch Data From Multiple Models In One View

This seems easy but I can't figure out how to do it

I'm trying to implement Ransack search in my rails app and for that I have a generated a model Cofounder which don't have any fields in it and I have association between Cofounder and CustomUser model which has email and other fields but i only want to search through email using ransack, i have this association between these two:

has_many :cofounders
belongs_to :CustomUser

(Do i need to have id if Yes how do i do it)

CoFounder Model

class Cofounder < ActiveRecord::Base
    belongs_to :CustomUser  
end

CofoundersController

class CofoundersController < ApplicationController

    layout "custom_layout", only: [:index]

    def index
        @q = Cofounder.ransack(params[:q])
        @cofounders = @q.result
        @cofoundes = CustomUser.all
        @countries = Country.all
    end
end

and in my index view

<% search_form_for @q, url: cofounders_index_path do |f| %>
    <%= f.search_field :email_cont, placeholder: "Search Here" %>
      <%= f.submit "Search" %>
<% end %>

<%= select_tag "search_by_country", options_for_select(@countries.collect{|x| [x.name]} , params[:search_by_country] ),{prompt: "Select country"} %><br><br>

<%= select_tag "choose_a_role", options_for_select(@cofoundes.collect{|x| [x.first_name]} , params[:choose_a_role] ),{prompt: "Choose A Role", id: "select", multiple: true} %><br><br>

 <% @cofoundes.each do |cofounder| %>
   <%= cofounder.email %>
 <%end%>

it is giving me error code

undefined method `email_cont' for #

i know email_cont if not a field in cofounder and i'm accessing that field in cofounder which is giving me error, how do i get those values in cofounder for successful Ransack search.

any help would be appreciated :)

Aucun commentaire:

Enregistrer un commentaire