Solr not working Rails - ruby-on-rails

I have this on my view:
<div class='container'>
<div class='row upper_container'>
<div class='search_container'>
<%= form_tag deals_path, :method => :get, :class => 'navbar-form navbar-left' do %>
<div class='form-group'>
<%= text_field_tag :search, params[:search], class: 'form-control' %>
</div>
<%= submit_tag 'Search', :name => nil %>
<% end %>
</div>
</div>
<% #deals.each_with_index do |d, i| %>
<% if i % 3 == 0 %>
<div class='row middle_container'>
<% end %>
<div class='col-md-4'>
<div class='deal_container'>
<%= d.title %>
<img src='<%= d.photo %>', class='deal_img'>
</div>
</div>
<% if (i % 3 == 2) || (i == (#deals.length - 1)) %>
</div>
<% end %>
<% end %>
</div>
this in my controller:
class DealsController < ApplicationController
def index
# #deals = Deal.paginate(:page => params[:page])
#search = Deal.search do
fulltext params[:search]
end
#deals = #search.result
end
private
def deal_params
params.require(:deal).permit(:title)
end
end
and this in my model:
class Deal < ActiveRecord::Base
searchable do
text :title
end
end
when I want to do a seach by some word, like 'Treatment', the #deals variable, in the controller is null, but the param is being sent: Parameters: {"utf8"=>"✓", "search"=>"Treatment"}
any idea?

Try this:
query = params[:search]
#search = Deal.search do
fulltext query
end
#deals = #search.result
Please check this answer for details.

Related

Ruby on Rails form error not giving any output/ not working

Hello guys I searched everywhere on how to output form errors but no luck. I tried almost all the code I found on the internet but still didn't work.
Here are some of my files:
view/books/new.html.erb
<div class="container">
<h2>Create a new book</h2>
<%= form_with model: #book, url: create_new_book_path do |f| %>
<% if #book.errors.any? %>
<ul>
<% #book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="form-group row">
<%= label_tag(:title, "Title:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.text_field :title, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:price, "Price:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.text_field :price, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:subject_id, "Subject:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.collection_select :subject_id, #subjects, :id, :name, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:description, "Book description:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.text_area :description, class: "form-control" %>
</div>
</div>
<%= submit_tag "Create book", class: "btn btn-success" %>
<%= link_to "Cancel", books_path, class: "btn btn-danger" %>
<% end %>
<br/>
</div>
books_controller.rb
class BooksController < ApplicationController
def index
#books = Book.all
end
def show
#book = Book.find(params[:id])
end
def new
#book = Book.new
#subjects = Subject.all
end
def create
#book = Book.new(book_params)
end
def book_params
params.require(:book).permit(:title, :price, :subject_id, :description)
end
def edit
#book = Book.find(params[:id])
#subjects = Subject.all
end
def update
#book = Book.find(params[:id])
if #book.update_attributes(book_params)
redirect_to action: "index"
else
#subjects = Subject.all
render action: "edit"
end
end
def destroy
Book.find(params[:id]).destroy
redirect_to action: "index"
end
end
routes.rb
get 'books/new', to: 'books#new', as: 'new_book'
post 'books/create', to: 'books#create', as: 'create_new_book'
view/layouts/application.html.erb
<main role="main">
<%= yield %>
</main>
I don't really know what I'm missing to get the errors, grateful for your answers!!
try putting them in a flash like this and render it in your view
def create
#book = Book.new
if #book.update(book_params)
flash[:notice] = 'success'
redirect_to action: 'index'
else
flash[:alert] = #book.errors.full_messages.join(', ')
redirect_to action: 'index'
end
end
and in your view render those flashes like
<% if flash[:notice]%>
<span class='notice'><%= flash[:notice] %></span>
<% end %>
<% if flash[:alert]%>
<span class='alert'><%= flash[:alert] %></span>
<% end %>

ruby on rails how to order by selection box

View:
<div>
<%= select_tag(:sorttitle, options_for_select(["title","publish_year"])) %>
<%= select_tag(:sortway, options_for_select(["Order By Asc","Order By Desc"])) %>
<%= submit_tag"Sort Books", class:"btn btn-info" %>
</div>
Model:
if(params[:sortway]=="Order By Desc")
#books=#books.order(params[:sorttitle]: :desc)
else
#books=#books.order(params[:sorttitle]: :asc)
end
#books = #books.order(params[:sorttitle] => :desc)

undefined method `any?' for nil:NilClass 2

i'm trying to show all the videos in my app so when i'm adding a new video and want to redirect to movie_path so i did this in my video controller:
def create
#video = Video.new(video_params)
if #video.save
flash[:success] = 'Video added!'
redirect_to movie_path(#movies)
else
render :new
end
end
it gives me an error:
undefined method `any?' for nil:NilClass
this is my show page that want to show the video:
<% if #videos.any? %>
<div class="container">
<div id="player-wrapper"></div>
<% #videos.in_groups_of(3) do |group| %>
<div class="row">
<% group.each do |video| %>
<% if video %>
<div class="col-md-4">
<div class="yt_video thumbnail">
<%= link_to image_tag("https://img.youtube.com/vi/#{video.uid}/mqdefault.jpg", alt: video.title,
class: 'img-rounded'),
"https://www.youtube.com/watch?v=#{video.uid}", target: '_blank' %>
<div class="caption">
<h5><%= video.title %></h5>
<p>Published at <%= video.published_at.strftime('%-d %B %Y %H:%M:%S') %></p>
<p>
<span class="glyphicon glyphicon glyphicon-thumbs-up"></span> <%= video.likes %>
<span class="glyphicon glyphicon glyphicon-thumbs-down"></span> <%= video.dislikes %>
</p>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
<% end %>
this is movie controller:
def index
#movies = Movie.all.order(:cached_votes_score => :desc)
#movies = #movies.paginate(:page => 1, :per_page => 8)
end
def show
#reviews = Review.where(movie_id: #movie.id).order("created_at DESC")
end
def new
#movie = current_user.movies.build
#movie = Movie.new
#categories = Category.all.map{|c| [ c.name, c.id ] }
end
and this is the routes i have:
nil don't have method any? so you must protect your code against it
instead this line:
<% if #video.any? %>
write this:
<% if #video.try(:any?) %>
I think you have not defined #videos, you have defined #video, so you should try this:
<% if #video.any? %>

Multiple records not available when creating .CSV

#orders should have multiple orders yet when using a respond_to to create an CSV, I only have one order record available in my cvs.erb file. Here's the code.
reports_controller.rb
def orders
params[:q] = {} unless params[:q]
if params[:q][:completed_at_gt].blank?
params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month
else
params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month
end
if params[:q] && !params[:q][:completed_at_lt].blank?
params[:q][:completed_at_lt] = Time.zone.parse(params[:q][:completed_at_lt]).end_of_day rescue ""
end
params[:q][:s] ||= "completed_at desc"
#search = Order.complete.ransack(params[:q])
#orders = #search.result
respond_to do |format|
format.html
format.csv do
headers['Content-Disposition'] = "attachment; filename=\"Order-List\"#{Date.today}"
headers['Content-Type'] ||= 'text/csv'
end
end
end
order_items.csv.erb
<%- headers = ['Order Number', 'User', 'Item Total', 'Discount Value', 'Sales Total', 'Promotions'] -%>
<%= CSV.generate_line headers %>
<%- #orders.each do |order| -%>
<%= CSV.generate_line([order.number, order.email, order.item_total, order.promo_total, order.total]) %>
<%- end -%>
order_items.html.erb
<%= search_form_for #search, :url => spree.order_items_admin_reports_path do |s| %>
<div class="form-group date-range-filter">
<%= label_tag nil, Spree.t(:date_range) %>
<div class="date-range-filter row">
<div class="col-md-6">
<%= s.text_field :completed_at_gt, :class => 'datepicker datepicker-from form-control', :value => datepicker_field_value(params[:q][:completed_at_gt]) %>
</div>
<div class="col-md-6">
<%= s.text_field :completed_at_lt, :class => 'datepicker datepicker-to form-control', :value => datepicker_field_value(params[:q][:completed_at_lt]) %>
</div>
</div>
</div>
<div class="form-actions">
<%= button Spree.t(:search), 'search' %>
</div>
<%= link_to "Export CSV", order_items_admin_reports_path(format: 'csv', :params[:q] => params[:q]), {:style => 'margin-top: 50px; margin-bottom: 20px;', :class => "btn btn-primary btn-success"} %>
<% end %>

Undefined method 'each' for nil class in create method

I'm getting an undefined method 'each' for nil class in my create method for a join table that I have.
I've got one join table for Emotions_pins and one for casues_pins the emotions pins table works fine but I'm getting the error on causes. Here's the code
_form.html.erb for Pin
<%= form_for(#pin) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<% if #pin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#pin.errors.count, "error") %> prohibited this checkin from being saved:</h2>
<ul>
<% #pin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h3>Hi! Thank you for choosing to check-in with your teacher! This is a great way to get help, share your feelings and concerns, and make your school a safer place to learn. </h3>
<div class="form-group">
<%= label_tag(:classroom, "Select your classroom:") %>
<%= select_tag "pin[code]", options_from_collection_for_select(Classroom.all, "code", "code", ) %>
</div>
<h4>Where?</h4>
<% #causes.each do |cause| %>
<div class="checkbox">
<ul>
<li>
<%= check_box_tag "reflection[cause_ids][]", cause.id %>
<%= label_tag(cause.name) %>
</li>
<ul>
</div>
<% end %>
<div class="form-group">
<%= image_tag 'feelings.png', class: "image" %>
<h4>How are you feeling?</h4>
</div>
<% #emotions.each do |emotion| %>
<div class="checkbox">
<%= check_box_tag "pin[emotion_ids][]", emotion.id %>
<%= label_tag(emotion.name) %>
</div>
<% end %>
<div class="form-group">
<h4> You can <strong>free write </strong> </h4>
<p> I want my teacher to know _____________________________________.</p>
<%= f.text_area :question, class: "form-control" %>
</div>
<div class="form-group">
<h4> You can write about your own actions or thoughts here.</h4>
<p>Something I did was ________________________________.</p>
<%= f.text_area :question1, class: "form-control" %>
</div>
<div class="form-group">
<h4>You can write about the actions of another person here..</h4>
<p>Something __(name)_____did was___________________________________.</p>
<%= f.text_area :question2, class: "form-control" %>
</div>
<div class="form-group">
<h4>Do you have a question for your teacher?</h4>
<p>I want to ask my teacher ______________________________________.</p>
<%= f.text_area :question3, class: "form-control" %>
</div>
<div class="form-group">
<h4>Are you thinking about <strong>doing something else</strong>? You can write about it here.</h4>
<p>Something else I might do is ______________________________________.</p>
<%= f.text_area :question4, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "submit", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
EDIT [ ADDED pin_controller.rb]
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
respond_to :html s
def home
#pins = Pin.all
respond_with(#pins)
authorize #pins
end
def show
respond_with(#pin)
end
def new
#pin = Pin.new
#emotions = Emotion.all
#causes = Cause.all
#school = School.find(params[:school])
respond_with(#pin)
authorize #pin
end
def edit
end
def create
code = params[:pin][:code]
#classroom = Classroom.where('code LIKE ?', code).first
unless #classroom
flash[:error] = "Classroom code incorrect"
#emotions = Emotion.all
#causes = Cause.all
render :new
else
params[:pin][:classroom_id] = #classroom.id
#pin = Pin.new(pin_params)
#pin.save
params[:pin][:cause_ids].each do |cause_id|
#cause = Cause.find(cause_id)
#pin.causes << #cause
end
params[:pin][:emotion_ids].each do |emotion_id|
#emotion = Emotion.find(emotion_id)
#pin.emotions << #emotion
end
if #pin.save
redirect_to signout_path and return
end
respond_with(#pin)
authorize #pin
end
end
def update
#pin.update(pin_params)
respond_with(#pin)
authorize #pin
end
def destroy
#pin.destroy
respond_with(#pin)
authorize #pin
end
private
def set_pin
#pin = Pin.find(params[:id])
authorize #pin
end
def pin_params
params.require(:pin).permit(:user_id, :question, :question1, :question2,
:question3, :question4, :question5, :classroom_id, :sad,
:happy, :mad, :brave, :embarrassed, :sorry, :frustrated,
:silly, :left_out, :excited, :hurt, :jealous, :confused,
:proud, :other)
end
end
Here's the exact error I'm getting
I've so far been unable to figure out the problem. What am I missing?
Stupid error but didn't catch it until I posted the form code... Thanks for the help!!
"reflection[cause_ids][]"
should be
"pin[cause_ids][]"
Thanks again for the help:)
<h4>Where?</h4>
<% #causes.each do |cause| %>
<div class="checkbox">
<ul>
<li>
<%= check_box_tag "reflection[cause_ids][]", cause.id %>
<%= label_tag(cause.name) %>
</li>
<ul>
</div>
<% end %>

Resources