Search with Ruby on Rails - html

I am currently learning Ruby on Rails, however, I have run into a slight issue, I am unable to create a text field from which to search. I so far have a working DB and web page (albeit an ugly one), however, I am having a problem with this simple function, here is my code so far:
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<strong>Search Title:</strong>
text_field_tag(:id)
<%= #article = Article.find(params[:id]) %>
<% end %>
</table>
error
/Users/jake/Documents/internships/rails/search/app/views/articles/index.html.erb:31: syntax error, unexpected keyword_ensure, expecting end-of-input
just for the web page, any help would be strongly appreciated.
Thanks in advance!

Try looking through this code:
In your html:
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
In your controller:
def index
#projects = Project.search(params[:search])
end
and in the model
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Here is the link
Also as I'm doing in my project, you can create separate action in your index/welcome controller called search:
def search
end
Then configure route in your routes.rb like so(not sure exactly this will work)
get 'welcome/' => 'welcom#search', as: 'search'
And then in view create a form:
<%= form_tag search_path, method: 'get', remote: true, id: "garage-search" %>
<%= text_field_tag :search %>
<% end %>
This will send search request by AJAX to your search controller. There you can process it as needed.
Also try look here. Very clearly explained..

Related

How to update chart (Chartkick) on a page after filtering?

I have a problem. I have made some filters using ransack and they work fine, but I have no idea how to update the pie chart that I have implemented under the table that I filter. The chart is made using Chartkick. I am fairly new to ror and programing in general. Here is my index file:
<h1>Lista Przychodów</h1>
<h2><%= link_to 'Nowy Przychód', new_income_path %></h2>
<%= search_form_for #q do |f| %>
<div class="field">
<%= f.label :title_cont, "Tytuł zawiera" %>
<%= f.search_field :title_cont %>
</div>
<div class="field">
<%= f.label :text_cont, "Opis zawiera" %>
<%= f.search_field :text_cont %>
</div>
<div class="field">
<%= f.label :category_name_cont, "Kategoria zawiera" %>
<%= f.search_field :category_name_cont %>
</div>
<div class="field">
<%= f.label :amount_gteq, "Kwota pomiędzy" %>
<%= f.search_field :amount_gteq %>
<%= f.label :amount_lteq, "a" %>
<%= f.search_field :amount_lteq %>
</div>
<%= f.submit "Szukaj" %>
<div class="button">
<%= link_to 'Wyczyść filtry', request.path, class:"cancel-button" %>
</div>
<% end %>
<div style="overflow-x:auto;">
<table class="center">
<tr>
<th><%= sort_link(#q, :title, 'Tytuł', default_order: :desc) %></th>
<th><%= sort_link(#q, :text, 'Opis') %></th>
<th><%= sort_link(#q, :amount, 'Kwota') %></th>
<th><%= sort_link(#q, :category_id, 'Kategoria') %></th>
<th colspan="3"></th>
</tr>
<% #incomes.each do |income| %>
<tr>
<td><%= income.title %></td>
<td><%= income.text %></td>
<td><%= number_to_currency(income.amount, :unit => "zł", :format => "%n %u") %></td>
<td><%= Category.find(income.category_id).name %></td>
<td><%= link_to 'Pokaż', income_path(income) %></td>
<td><%= link_to 'Edytuj', edit_income_path(income) %></td>
<td><%= link_to 'Usuń', income_path(income),
method: :delete,
data: { confirm: 'Czy na pewno chcesz usunąć ten wpis?' } %></td>
</tr>
<% end %>
</table>
</div>
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
<footer><%= link_to 'Strona główna', controller: 'welcome' %></footer>
And my controller file if it is needed:
class IncomesController < ApplicationController
helper_method :sort_column, :sort_direction
http_basic_authenticate_with name: "admin", password: "admin", except: [:index, :show]
def index
#q = Income.search(params[:q])
#incomes = #q.result.includes(:category)
end
def show
#income = Income.find(params[:id])
end
def new
#income = Income.new
#categories = Category.all.map { |c| [c.name, c.id]}
end
def edit
#income = Income.find(params[:id])
#categories = Category.all.map { |c| [c.name, c.id]}
end
def create
#income = Income.new(income_params)
#categories = Category.all.map { |c| [c.name, c.id]}
if #income.save
redirect_to #income
else
render 'new'
end
end
def update
#income = Income.find(params[:id])
if #income.update(income_params)
redirect_to #income
else
render 'edit'
end
end
def destroy
#income = Income.find(params[:id])
#income.destroy
redirect_to incomes_path
end
private
def income_params
params.require(:income).permit(:title, :text, :amount, :category_id)
end
def sort_column
params[:sort] || "title"
end
def sort_direction
params[:direction] || "asc"
end
end
I already looked on the internet for a similar problem but didn't find anything. I would appreciate some help with that or at lest some directions what to do. I got really hooked on ror and would like to learn more about it, that's why I came here for help. Thanks in advance for answers.
If you mean the filters are applied on the table but not on the chart, this is because you are making another query for the chart only, in this line:
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
You should use the same instance variable that has the filtered data:
#incomes
So it should be something like:
<%= pie_chart #incomes.sum(:amount), suffix: " zł", thousands: ",", messages: { empty: "Nie ma wpisów" }, download: { filename: "wykres" }, title: "Podział przychodów na kategorie" %>

Why is there a text node that contains the database record inside the html page?

so on my website, there is an unwanted text node that contains the record of my databse. There is no tag or element whatsoever on the html code, but somehow it exist.
I reckon it is caused by this line of code:
<%= #workspace.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.owner %></td>
<td><%= item.quantity %></td>
<td><%= item.details %></td>
<td><%= link_to 'Edit Item', edit_workspace_item_path(#workspace,item) %></td>
<td><%= link_to 'Delete', [item.workspace, item],
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
The view.html.erb:
<p>
<strong>Name:</strong>
<%= #workspace.name %>
</p>
<p>
<strong>maxitem:</strong>
<%= #workspace.max %>
</p>
<p>
<strong>details:</strong>
<%= #workspace.details %>
</p>
<p>
<%= link_to 'Edit Phase', edit_workspace_path(#workspace) %>
</p>
<p><strong>Items</strong>
</p>
<p>
<%= link_to 'Add Item', new_workspace_item_path(#workspace) %></br>
</p>
<table>
<tr>
<tr>
<th><strong>Name</strong>
<th><strong>Owner</strong>
<th><strong>Quantity</strong>
<th><strong>Details</strong>
</tr>
<%= render 'items/item' %>
</table>
<%= link_to 'Back to List of Phases', workspaces_path %>
I don't know why this suddenly show up on my website
like this
<%= #workspace.items.each do |item| %>
is the issue as <%= %> in ERB stands for "print this". Should be <% %> which will execute the Ruby code without rendering it.

View Change "On Submit" Rails

Is there a way to do this without javascript?
Let me explain. I have two models, users and dogs.
I'm using the index as a home page for when the users are logged in.
There is a form that searches dogs (not by any information the user enters, it's prepopulated with dog data), but the index is listing the result of my search query on page load. I'd like it to only show when the button is submitted.
I'll show some code.
controller for dog:
def index
if current_user
#user = current_user
#breed = #user.dogs.first.primarybreed
# params[:search] = #breed
#dog = Dog.search(params[:search]).sample
end
end
view:
%= form_tag dogs_path, :method => :get do %>
<%= hidden_field_tag :search, #breed %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
<div id="dogswrap">
<h1><%= current_user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<td><%= image_tag #dog.image.url(:medium) %></td>
<td><%= #dog.name %></td>
<td><%= #dog.nick %></td>
<td><%= #dog.primarybreed %></td>
<td><%= #dog.secondarybreed %></td>
<td><%= #dog.age %></td>
<td><%= #dog.weight %></td>
<td><%= link_to "#{#dog.user.name}" %> </td>
<td><%= image_tag #dog.user.image.url(:thumb) %> </td>
</td>
</tr>
</tbody>
</table>
Model
def self.search(search)
find(:all, :conditions => ['primarybreed LIKE ?', "%#{search}%"])
end
I would like to have an if statement that shows the table only after the submit has been pushed.
If there is another way to do this, I'd appreciate it.
Thanks!
EDIT: Still working on this. May use a render..?
You can just use where in place of the way you're currently doing it find and conditions
def self.search(search)
where('primarybreed LIKE ?', "%#{search}%")
end
One thing I've learnt when using the LIKE function in development for SQLite3 and LIKE in PostgreSQL for production, was LIKE is case-insensitive in SQLite3 but not for PSQL. You would need to use ILIKE for that.
To your main question. This is how I would do it.
def index
if current_user
#user = current_user
#dogs = Dog.search(params[:search]) if params[:search].present?
end
end
For you views, start moving everything into a partial. _form.html.erb and _dog.html.erb
form
<%= form_tag dogs_path, :method => :get do %>
<%= text_field_tag :search %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
dog
<td><%= image_tag dog.image.url(:medium) %></td>
<td><%= dog.name %></td>
<td><%= dog.nick %></td>
<td><%= dog.primarybreed %></td>
<td><%= dog.secondarybreed %></td>
<td><%= dog.age %></td>
<td><%= dog.weight %></td>
<td><%= link_to "#{dog.user.name}" %> </td>
<td><%= image_tag dog.user.image.url(:thumb) %></td>
Notice removed #dog and replaced with dog
Index view
<%= render 'form' %>
<div id="dogswrap">
<h1><%= #user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<% if #dogs %>
<%= render #dogs %>
<% end %>
</tr>
</tbody>
</table>
changed current_user.dogs[0].. to #user.dogs[0].. Also made a conditional for if the #dogs instance variable is truthy. If it is, then render will render out the _dog.html.erb partial for each dog in the #dogs collection

Ruby on Rails how to include a form in an index view

I've been running my head into a wall for hours now and I can't make this work. I'm trying to add a bunch of check boxes to verify items on my index page. I found this old rails cast that does exactly what I want to do, but I've run into a problem. Anything enclosed inside my form is removed from the index page, like just gone poof. Here's the code from the index view.
<% form_tag verify_products_path, :method => :put do %>
<% #products_unverified.each do |products| %>
<% if product.deleted != 'true' %>
<tr data-link="<%= product_path(product) %>">
<td><%= product.name %></td>
<td><%= product.description %></td>
</tr>
<% end %>
<% end %>
<%= submit_tag "Mark as Verified" %>
<% end %>
</tbody>
Here's the routes stuff
resources :products do
put :verify, :on => :collection
end
and the controller just has a dummy method for now.
def verify
end
Any clue as to why the index view blanks out when the form is introduced? Any help is much appreciated.
You forgot the = before form_tag:
<%= form_tag verify_products_path, :method => :put do %>
put an = in front of form_tag
instead of
<% form_tag verify_products_path, :method => :put do %>
try
<%= form_tag verify_products_path, :method => :put do %>

Hiding html elements

I'm attempting to hide the links for edit and destroy actions in the view unless the user has logged in using http basic auth. I've attached my files below. Thanks
View https://gist.github.com/1272716
Controller https://gist.github.com/1272712
You need to save/store the authenticate result, and then render conditionally in your view.
Controller:
protected
def authenticate
#authenticated = authenticate_or_request_with_http_basic do |user, password|
user == "x4556d4s" && password == "fd55sas64x"
end
end
View:
<%= link_to "New Link", :controller => :links, :action => :new %>
<table>
<% #links.each do |link| %>
<tr>
<td> <%= link_to '+', up_link_url(link), :method => :put %> <%= link.points %> <%= link_to '-', down_link_url(link), :method => :put %> </td>
<td> <%= link.title %> </td>
<% if #authenticated %>
<td><%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%= link_to 'Edit', edit_link_path(link) %></td>
<% end %>
</tr>
<% end %>
</table>