Searching with mongoid search - ruby-on-rails

i have a 3 models user ,user_role and store .user_role belongs to user and store.i have a member controller where i have a search box one for stores,one for user_role(both are select box)below is method for search in user_role model
def self.filter(params)
filter = params[:search] || {}
user_roles = order(role_type: :asc)
if filter[:name].present?
end
if filter[:email].present?
end
if filter[:role].present?
user_roles = user_roles.where(role_type: filter[:role])
end
if filter[:store_id].present?
user_roles = user_roles.where(store_id: filter[:store_id])
end
user_roles
end
This is my member controller below
def index
if current_user.super_admin?
#total_user_roles = UserRole.filter(params).order(sort_column + " "
+ sort_direction)
#user_roles = #total_user_roles.page(params[:page]).per(10)
else
stores = current_user.user_roles.where(:role_type.in =>
['store_manager', 'merchant']).pluck(:store_id)
#total_user_roles = UserRole.where(:store_id.in =>
stores).filter(params).order(sort_column + " " + sort_direction)
#user_roles = #total_user_roles.page(params[:page]).per(10)
end
respond_to do |format|
format.html
format.csv { send_data #users.to_csv }
end
end
def sort_column
User.attribute_names.include?(params[:sort]) ? params[:sort] :
"first_name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] :
"asc"
end
My search form in view.Should i change anything here in name and email field or just leave it as itis?
<section>
<div class="col-md-12">
<div class="row">
<section class="content-header">
<div class="white card-2">
<h3>Users</h3>
</div>
</section>
</div>
<div class="form white">
<!-- form start -->
<form class="form-horizontal">
<div class="box-body">
<h3>Search Users </h3>
<div class="col-md-3">
<label class="control-label"
for="order_search_order_number">First Name</label>
<input class="form-control" id="order_search_order_number"
name="search[name]" type="text" value="">
<span class="md-input-bar"></span>
</div>
<div class="col-md-3">
<label class="control-label">Email</label>
<input class="form-control" id="order_search_order_number"
name="search[email]" type="text">
<span class="md-input-bar"></span>
</div>
<div class="col-md-3 input">
<label class="control-label">Roles</label>
<%= select_tag("search[role]",
options_for_select(User::ADMIN_ROLES.map { |e| [e.humanize, e]},
params[:search] && params[:search][:role]), include_blank: true,
class: 'select2') %>
<span class="md-input-bar"></span>
</div>
<div class="col-md-3">
<label class="control-label">Store</label>
<%= select_tag("search[store_id]",
options_for_select(Store.all.order('name ASC').map { |e|
[e.name, e.id] }, params[:search] && params[:search][:store_id]),
include_blank: true, class: 'select2') %>
<span class="md-input-bar"></span>
</div>
</div>
<h4 class="pull-right"> Total Users: <b> <%=
#total_user_roles.count %></b>
</h4>
<br><br>
<div class="box-footer">
<button type="submit" class="btn btn-info">Filter</button>
Clear
</div>
<!-- /.box-footer -->
</form>
</div>
</div>
</section>
<div class="col-md-12">
<section class="section-data">
<!-- Section Data Table -->
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-body table-responsive custom-sort">
<table class="table table-striped table-bordered nowrap data-
table" cellspacing="0" width="100%">
<thead>
<tr>
<th><%= sortable 'first_name', "Name" %></th>
<th><%= sortable 'email' %></th>
<th>Store</th>
<th><%= sortable 'role' %></th>
<th>Status</th>
<th>Action</th>
<th>Active?</th>
</tr>
</thead>
<tbody>
<% #user_roles.each do |role| %>
<tr>
<td><%= role.user.name%></td>
<td><%= role.user.email %></td>
<td><%= role.store.try(:name) %></td>
<td><%= role.role_type.try(:humanize) %></td>
<td><%= role.user.pending_invitation? ? "Pending" :
"Accepted" %></td>
<td>
<a class="ion-edit actions" href="/admin/members/<%=
role.id %>/edit"></a>
<% if role.user != current_user %>
<a class="ion-android-delete actions red" data-
sweet-alert-confirm="<%= t(:delete_confirm) %>" data-
method="delete" href="/admin/members/<%= role.user.id %>"></a>
<% end %>
</td>
<td>
<div class="onoffswitch">
<input type="checkbox" name="active" <%= "checked" if
role.user.active %> class="onoffswitch-checkbox" id="<%=
role.user.id %>" data-id="<%= role.user.id %>">
<label class="onoffswitch-label" for="
<%=role.user.id %>">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</tr>
<% end %>
</tbody>
</table>
<div class="pull-right">
<%= paginate #user_roles, :theme => 'twitter-bootstrap-3' %>
</div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
</section>
</div>
now i want to implement search for user name and email ,i tried lots of ways but cant come up with solution.as Iam not expert need some solution from a expert .iam using mongoid search gem.

Related

Rails 5 yield gives duplicate contents

I am figuring out why it showing duplicate data on the front page:
All I did is to use Rails collection rendering.
Here's my application.html.erb file:
<body>
<%= render 'layouts/navbar' %>
<!-- content -->
<main class="pt-5">
<div class="container">
<div class="row">
<%= render 'layouts/sidebar' %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</main>
</body>
</html>
Here's my index.html.erb file:
<div class="col-md-9">
<% if #contacts.blank? %>
<div class="alert alert-warning">
<strong>Record is empty!</strong>
</div>
<% else %>
<div class="card">
<div class="card-header"><strong>All Contacts</strong></div>
<table class="table">
<%= render #contacts %>
</table>
</div>
</div>
<% end %>
and here's my _contact.html.erb file:
<% #contacts.each do |contact| %>
<tr>
<td class="middle">
<div class="media">
<div class="media-left">
<a href="#">
<%= image_tag contact.gravatar, class: "media-object" %>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><%= contact.name %></h4>
<address>
<strong><%= contact.address %>, <%= contact.city %>, <%= contact.state %>, <%= contact.zip %></strong><br>
<%= contact.email %> | <%= contact.phone %> | <%= contact.mobile %>
</address>
</div>
</div>
</td>
<td width="100" class="middle">
<div>
<a href="#" class="btn btn-outline-secondary btn-circle btn-xs" title="Edit">
<i class="fa fa-edit"></i>
</a>
<a href="#" class="btn btn-outline-danger btn-circle btn-xs" title="Delete">
<i class="fa fa-times"></i>
</a>
</div>
</td>
</tr>
<% end %>
BTW, I am using Kaminari Gem for pagination. It works however the data shows twice.
Any idea?
UPDATES:
Here's my contacts controller:
class ContactsController < ApplicationController
def index
#contacts = Contact.page params[:page]
end
end
Here's the kaminari config file:
# frozen_string_literal: true
Kaminari.configure do |config|
config.default_per_page = 2
# config.max_per_page = 4
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
# config.params_on_first_page = false
end
When you do <%= render #contacts %>, Rails will loop results from #contacts and render each one using _contact.html.erb, while embedding local variable contact in each one. You see contacts twice, because you loop twice: remove <% #contacts.each do |contact| %> from _contact.html.erb and it should work.

Update or Remove Item from Cart?

I manage the orders made by the customers. My Order table is connected to the model Cart. The model Cart is connected to the Product model Many to Many which gave the association table line_items which includes id_cart, id_product, and quantity. But for the Cart, I want to add the option to update and delete an element of the Cart. I met a blocking - more precisely in the view Order where at the end I show the list of the elements in the Cart as follows
<h3> Order Details </ h3>
<table class = "table-bordered table">
<td> Designation </ td> <td> Quantity </ td> <td> Price </ td> </ tr>
<% order.cart.line_items.each do | item | %>
<tr align = "center">
<td> <% = f.text_field: product_id,: value => item.product.title,: readonly => true, class: 'form-control'%> </ td>
<td> <% = f.text_field: qte,: value => item.qte, class: 'form-control'%> </ td>
<td> <% = number_to_currency (product.inc.unit, unit: "F", separator: ",", format: "% n% u")%> </ td>
</ Tr>
<% end%>
</ Table>
There I will have the fields textfield with default value what the customer had ordered allowing the modification. But the problem is that the attibutes product_id and qte do not belong to the model order but earlier to the model line_items, so I will not have permission to insert:
unpermitted parameters: :product_id, :qte
I even try the method accepts_nested_attributes_for and I will have permission. But the transaction will not commit but rollback.
And thank you for helping me for this bug that prevents me from advancing for days
the submitted params look like this
<%= form_with(model: order,:remote => true) do |f| %>
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="header">
<h4 class="title"> Edite Order</h4>
</div>
<div class="content">
<div class="row">
<!-- Order Reference-->
<div class="col-md-5">
<div class="form-group">
<label><%= f.label :ref,"Reference Order "%></label>
<%= f.text_field :ref, :value => "ABC-123-NY#{#order.id}",:readonly => true,class: ' form-control' %>
</div>
</div>
<!-- Supplier List -->
<div class="col-md-5">
<div class="form-group">
<label><%= f.label :supplier_id,"Fournisseur"%></label>
<%=f.collection_select :supplier_id,Fournisseur.all,
:id,:namefrs,{required: true},id:"listfrs",class: 'form-control'%>
</div>
</div>
<!-- boutton update -->
<div class="col-md-2">
<div class="form-group">
<%= f.submit 'Modifier',:name => "valid",:class => 'btn btn-info btn-fill pull right'%>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="content">
<div class="row">
<!-- Date Order -->
<div class="col-md-5">
<div class="form-group">
<label><%= f.label :date_order,"Date"%></label>
<%= f.date_select :date_order, label: false, placeholder: "Sélectionnez la date et l'heure",class: 'form-control'%>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col" id="cart">
<h3>Order</h3>
<table class="table table-bordered">
<tr align="center" class="h4">
<td>Designation</td>
<td>Quantity</td>
<td>Price</td>
</tr>
<% order.cart.line_items.each do |item| %>
<tr align="center">
<td><%= f.text_field :product_id,:value => item.product.title,:readonly => true,class: 'form-control'%></td>
<td><%= f.text_field :qty, :value=>item.qty ,class: 'form-control' %></td>
<td><%= number_to_currency(item.product.price, unit: "F",separator:",",format:"%n %u")%></td>
</tr>
<% end %>
</table>
</div>
<% end %>
i have this in my controller Order
def update
respond_to do |format|
#order.cart.line_items.each do |item|
item.qty = params[:qty]
end
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
end
end

Iterate 4 Arrays with Rails

I'm new to Rails and I'm trying to build a nice application and I'm struggling with arrays, I have 4 arrays that I want to iterate and they are not the same size
I want to generate sections in HTML using the first array what I did it
#sections = ['Section One','Section Two','Section Three','Section Four']
#itemsOne = ['item 1','item 2','item 3','item 4','item 5','item 6']
#itemsTwo = ['item 1','item 2','item 3','item 4','item 5','item 6']
I was using
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t1, t2, t3| %>
<%= t1 %>
<table>
<tbody>
<tr>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
</tr>
</tbody>
</table>
<% end %>
I have a table that have a Section Title and cells that have two values
but what I get is the value of |t2| in each cell of |t1| section
using #Phil answer down below, but he deleted it.
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t| %>
<%= t[0] %>
<table>
<tbody>
<tr>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
</tr>
</tbody>
</table>
<% end %>
p.s. the itemsOne and itemsTwo arrays have more than 20 values.
What I created is separated my big arrays into smaller ones and then Iterated thru each this way without a Table because table was making design issues so i went into div's using bootstrap 3 column, there might be a better way but this is what i got as a beginner.
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[0] %></h4>
<!-- This will Display Section 0 in the Array -->
</div>
<div class="row">
<div class="col-md-12">
<% #count = 0 %>
<!-- Counter is Zero -->
<% #ItemsOne.collect do |t1| %>
<!-- This will loop array to increment the #count and repeat the HTML -->
<% #count += 1 %>
<!-- With each loop increment by 1-->
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #ItemsOne[#count - 1] %>
<!-- Counter should start from 0 adding -1 will make it start
from 0 instead of 1 and then will print the value of the Index Number -->
</label>
</div>
</div>
<% end %>
</div>
</div>
</div>
Here is another way to do this
<div class="row">
<% #Sections.each_with_index do |x1, n| %>
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[n] %></h4>
</div>
<div class="row">
<div class="col-md-12">
<% if n == 0 %>
<% #itemsOne.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsOne[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 1 %>
<% #itemsTwo.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsTwo[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 2 %>
<% #itemsThree.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsThree[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 3 %>
<% #itemsFour.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsFour[n] %>
</label>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>

Table in partial view not rendering

I am trying to display a table of jobs for a client sectioned by month year. The partial views evaluate and using binding.pry on each I can see that the correct values are there. The _month_section.html.erb date does not show, nor does the table in _jobs.html.erb
clients_controller.rb
def show
if id = params[:id]
if #client = current_user.clients.find_by_id(id.to_i)
if #client.jobs.count > 0
#jobs_by_month = #client.jobs.group_by{|j| j.created_at.strftime('%B %Y')}
end
else
redirect_to dashboard_path
end
end
end
show.html.erb
<% if #client.jobs.count > 0 %>
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="panel panel-white">
<div class="panel-heading">
<h4 class="panel-title">Jobs</h4>
<div class="panel-body">
<%= render 'month_section' %>
</div>
</div>
</div>
</div>
</div>
<% end -%>
_month_section.html.erb
<div class="month_section" >
<% #jobs_by_month.each do |date, jobs| %>
<div>
<h3><%= date %></h3>
<%= render partial: 'jobs', :locals => {:jobs => jobs } %>
</div>
<% end %>
</div>
_jobs.html.erb
<div class="jobs">
<div class="row">
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Date</th>
<th>Start Time</th>
<th>End Time</th>
</tr>
</thead>
<tbody>
<% jobs.each do |job| %>
<tr>
<td><%= job.date%></td>
<td><%= job.start_time %></td>
<td><%= job.end_time %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
Turns out issue was due to a rogue non-terminated div tag on show.html.erb outside of the snippet I posted.

Problems with OpenForum Index.ascx?

This is the ascx page:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<OpenForum.Core.ViewModels.IndexViewModel>" %>
<%# Import Namespace="OpenForum.Core.Views.ForumViewHelper" %>
<div class="openforum_container">
<%= (Model.IncludeDefaultStyles) ? ForumViewHelper.GetDefaultStyles() : ""%>
<%= (Model.IncludeValidationSummary) ? Html.ValidationSummary() : ""%>
<div class="openforum_actions">
<%= Html.ActionLink("Write A New Post", "Post") %>
</div>
<div class="openforum_search">
<% Html.BeginForm(); %>
Search: <%= Html.TextBox("searchQuery") %> <input type="submit" value="go" />
<% Html.EndForm(); %>
</div>
<div class="openforum_message"><%= Model.Message %></div>
<table class="openforum_maincontent">
<% foreach(var item in Model.Posts ?? new Post[0]) { %>
<tr>
<td class="openforum_title"><%= Html.ActionLink(item.Title, "view", new { id = item.Id, title = ForumViewHelper.ToUrlFriendlyTitle(item.Title) })%></td>
<td class="openforum_user">
<div>created by <%= Html.Encode(item.CreatedBy.Username) %></div>
<div><%= item.CreatedDate.ToString("MM/dd/yyyy hh:mm tt") %></div>
</td>
<td class="openforum_modified">
<div>last post by <%= Html.Encode(item.LastPostBy.Username) %></div>
<div><%= item.LastPostDate.ToString("MM/dd/yyyy hh:mm tt") %></div>
</td>
<td class="openforum_replies">Replies: <%= item.Replies.Length %></td>
<td class="openforum_views">Views: <%= item.ViewCount %></td>
</tr>
<% } %>
</table>
<div class="openforum_index_paging">
<% if ((Model.Posts ?? new Post[0]).Count() > 0) { %>
<span>Page <%= Model.CurrentPage + 1 %> of <%= Model.TotalPages %></span>
<% } %>
<% if (Model.CurrentPage > 0) { %>
<% Html.BeginForm(); %>
<input type="submit" value="<<<" />
<input type="hidden" name="searchQuery" value="<%= Model.SearchQuery %>" />
<input type="hidden" name="page" value="<%= Model.CurrentPage - 1 %>" />
<% Html.EndForm(); %>
<% } %>
<% if (Model.CurrentPage < Model.TotalPages - 1) { %>
<% Html.BeginForm(); %>
<input type="submit" value=">>>" />
<input type="hidden" name="searchQuery" value="<%= Model.SearchQuery %>" />
<input type="hidden" name="page" value="<%= Model.CurrentPage + 1 %>" />
<% Html.EndForm(); %>
<% } %>
</div>
</div>
I keep getting an error: The name 'ForumViewHelper' does not exist in the current context. I am using the OpenForum dll. I called the Html.RenderPartial("Index.ascx").
I think the issue is that "ForumViewHelper" is a class inside the "OpenForum.Core.Views" namespace. Try using this instead...

Resources