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 :)
Related
I got simple_form for testrun model with multiple checkboxes, that save an array of testcases in a model field
app/views/testruns/_form.html.erb
<%= simple_form_for #testrun do |f| %>
<%= f.input :testcase, as: :check_boxes,
collection: [["testcase1", :testcase1], ["testcase2", :testcase2], ... ]%>
<%= f.submit %>
<% end %>
It works fine, but from now I need to create another model called testcase. After submitting form, besides creating a new testrun instance, I need to create testcase instances which depends on every flag checked.
Any idea how can I do it?
You need to use accepts_nested_attributes_for and simple_fields_for. Assuming you have has_many :testcases in Testrun and the field name of Testcase is name, the below steps should put you in the right direction.
#app/models/testrun.rb
accepts_nested_attributes_for :testcases
#app/controllers/testrun_controller.rb
def new
#testrun = Testrun.new
#testrun.testcases.build
end
private
def testrun_params
params.require(:testrun).permit(:field1, :field2.., testcases_attrubtes: [name: []])
end
#app/views/testruns/_form.html.erb
<%= simple_form_for #testrun do |f| %>
<%= f.simple_fields_for :testcases do |testcase| %>
<%= testcase.input :name, as: :check_boxes,
collection: [["testcase1", :testcase1], ["testcase2", :testcase2], ... ]%>
<% end %>
<%= f.submit %>
<% end %>
hi so I'm trying to do 2 things, one of them is to basically redirect to a model's ID number so input is "1" and redirects to
localhost:3000/model/1
and the second part is actually doing a search. each model has a text field string for license_no and I want to be able to search and return results
currently, I am not sure how I would combine these into 1 search form if thats possible or if it would require 2 separate search forms
i have a search form with only the license_no field but it always returns no results found...
apologize that the model name isn't in singular, the guide I was using to learn RoR had it that way and everything worked, but I have so many references to renters in my project that it would be a while to find all of them
index.html.erb
<%= form_tag search_renters_path, method: get do |f| %>
<%= text_field_tag :license, nil, placeholder: "License Number" %>
<%= submit_tag 'Search', class: "btn-large" %>
<% end %>
models/renters.rb
class Renters < ActiveRecord::Base
validates_presence_of :license_no
def self.search(params)
renters = Renters.where("license_no LIKE?", "%#{params[:license]}%")
end
end
controller.rb
def search
#renter = Renters.search([params])
end
search.html.erb - snippet
<% if #renter.blank? %>
no results
<% else %>
#show results
<% end %>
editted code
models/renters.rb
def self.search(params)
license_query = "%#{params[:license]}%"
id_query = "%#{params[:id]}%"
renters = Renters.where("license_no LIKE ?", license_query) if params[:license].present?
renters = Renters.where("id LIKE ?", id_query) if params[:id].present?
end
controller
def search
#renter = Renters.search(params)
end
search form
<%= form_tag search_renters_path, method: :get do |f| %>
<%= text_field_tag :license, nil, placeholder: "Driver's License" %>
<%= text_field_tag :id, nil, placeholder: "ID number" %>
<% end %>
I'm trying to use the if present? statements to allow a user to decide whether to input ID No or License No. you don't need to input both just one. currently, if I search for a license no, it returns no results. but when I search for an ID, it returns the relevant result
you can do something like this if you are getting value on params[:licence] from your form submit on your controller action search
controller.rb
def search
#renter = Renters.search(params[:licence])
end
app/models/renters.rb
class Renters < ActiveRecord::Base
def self.search(query)
like_query = "%#{query}%"
renters = Renters.where("id LIKE ? OR license_no LIKE ?", like_query, like_query)
end
end
I have a model called "loans", which has many :vehicles. The model implementation for loan accepts nested attributes for vehicles through a nested form (provided through simple_form gem).
class Loan < ApplicationRecord
has_many :vehicles
accepts_nested_attributes_for :vehicles
end
.
class LoansController < ApplicationController
def new
#loan = Loan.new
#loan.vehicles.build
end
def edit
#loan = Loan.find(params[:id])
#vehicle = #loan.vehicles
end
end
Edit.html.erb:
<%= simple_form_for #loan, :html => {:class => "loan-edit-form"} do |f| %>
<%= f.simple_fields_for :vehicles do |vehicle| %>
<%= render 'vehicle_fields_edit', :f => vehicle %>
<% end %>
<% end %>
_session_fields_edit.html.erb:
<div class="nested_vehicle" data-id="<%= #vehicle.id %>">
<%= f.hidden_field :id, id: "vehicle_id", name: "vehicle_id" %>
<div class="divider-top"></div>
<div class="vehicle-number">
Vehicle <span class="vehicle-count-rails"><%= f.index + 1 %></span>
</div>
...... etc
</div>
The problem is I'm unable to show the database record ID of the vehicle in the edit nested form as I'm trying to do in the "data-id" attribute since I'm using simple form builder and not simply looping through the loan records using #loan.vehicles.each do |vehicle|.
Using a hidden field I'm able to show the record ID, however, it is shown within an input form field item, whereas I need to retrieve only the ID value alone for use within the data-id attribute.
I've tried <%= f.id %> but get a form builder error. How can I get the record ID whilst using form builder as I've described?
Try this, instead of f.id:
<%= f.object.id %>
I'm getting a little stuck with a ransack search form.
I need to search through a model's association, and bring results back when name_cont BUT ONLY WHEN AN associated model's status == something.
I have a gig model with a has_many belongs_to relationship with a request model and user model. The request model has attributes status and the user model has attributes publicname. I want to search publicname_cont and only show results from the requests that have a status == "hired".
Currently my search brings back the results of publicname regardless or the request models status.
Gigs Controller:
#gigs = #q.result(distinct: false).notexpired.order(date: :asc).page(params[:page]).per(20)
Search form:
<%= search_form_for #q, url: welcome_index_url, remote: false do |f| %>
<%= text_field_tag :search, params[:search], class: 'loc-search', placeholder: "Location"%>
<%= f.search_field :locationname_cont, placeholder: "Bar name" %>
<%= f.search_field :requests_user_publicname_cont, placeholder: "Band name" %>
<%= f.submit %>
<% end %>
Currently, I can search requests_user_publicname_cont and it gives me all the results as expected where the parameters match.
I am looking for a way to search requests_user_publicname_cont AND requests_status_eq, ("hired")
I am not sure how to go about this, any help would be appreciated, thanks.
#q = Gig.joins(:requests).where(requests: { status: 'hired' }).ransack(params[:q])
I'm working on my first rails app and having a weird issue.
I'm using Rails 3.2.6 with mongodb.
In my view, I can get a label to display like so:
<%= f.label :percent %>
But when I try the same thing with a text field
like so:
<%= f.text_field :percent %>
the page doesn't even load, nothing happens.
Here is my controller:
class TrimmingsController < ApplicationController
def new
#order = Order.find params[:order_id]
end
And my model:
class Trimming
include Mongoid::Document
embedded_in :order
field :percent, type: String
end
And here is my whole view:
<%= form_for #order do |f| %>
<p>
<%= f.text_field :percent %>
</p>
<% end %>
I just want to get a text_field to display sorry for the
simpleton question but I've been trying for 2 days to get
this text field to pop up.
You need to make percent attr_accessible. Add this to your model.
attr_accessible: :percent
Check it with following code in view:
<%= form_for :order do |f| %>
<%= f.text_field :percent %>
<% end %>