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
Related
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.
Hello i am new on ruby on rails.I am trying to build a page that have a date field, and after choosing a specific date, i want to generate a table that shows reports of this date select.
controllers/reports_controller.rb
class ReportsController < ApplicationController
def index
end
def test
#chosen_date = params[:report_date]
#incoming_messages = Message.where("inbound = 't' and created_at like '" + #chosen_date + "%'").count
#total_chats = Message.where("created_at like '" + #chosen_date + "%'").distinct.count(:phone_number)
#enrollment_by_andi = Enrollment.where("created_at like '" + #chosen_date + "%' and created_by = 'ANDI'").count
#enrollment_by_agent = Enrollment.where("created_at like '" + #chosen_date + "%' and created_by <> 'ANDI'").count
#sent_by_andi = Message.where("created_by = 'ANDI' and created_at like '" + #chosen_date + "%'").count
#sent_by_agents = Message.where("inbound = 'f' and created_by <> 'ANDI' and created_at like '" + #chosen_date + "%'").count
#unread_messages = Message.where("created_at like '" + #chosen_date + "%' and is_read = 'f'").distinct.count(:phone_number)
end
end
views/reports/index.html.erb
<h1>Reports</h1>
<%= form_tag(reports_test_path,:method => "post") do %>
<%= label_tag(:report_date,"Choose a Date") %>
<%= date_field_tag(:report_date)%>
<%= button_tag "Submit"%>
<% end %>
table i want to generate:
<table>
<th>
<td>Incoming Messages</td>
<td>Total Chats</td>
<td>Enrollment By Andi</td>
<td>Enrollment By Agent</td>
<td>Sent By Andi</td>
<td>Sent By Agents</td>
<td>Unread Messages</td>
</th>
<tr>
<td><%= #incoming_messages %></td>
<td><%= #total_chats %></td>
<td><%= #enrollment_by_andi %></td>
<td><%= #enrollment_by_agent %></td>
<td><%= #sent_by_andi %></td>
<td><%= #sent_by_agents %></td>
<td><%= #unread_messages %></td>
</tr>
</table>
Below is the image of the page:
Anyone can help me to find a method to generate a table under the date field when i press the button ?
You can use AJAX which goes really well with rails and is really decent. For this follow the following steps -
First create a partial _report_table.html.erb. You have a little problem in the table. So paste the below table html in the partial -
<table>
<tr>
<th>Incoming Messages</th>
<th>Total Chats</th>
<th>Enrollment By Andi</th>
<th>Enrollment By Agent</th>
<th>Sent By Andi</th>
<th>Sent By Agents</th>
<th>Unread Messages</th>
</tr>
<tr>
<td><%= #incoming_messages %></td>
<td><%= #total_chats %></td>
<td><%= #enrollment_by_andi %></td>
<td><%= #enrollment_by_agent %></td>
<td><%= #sent_by_andi %></td>
<td><%= #sent_by_agents %></td>
<td><%= #unread_messages %></td>
</tr>
</table>
create a js file as test.js.erb as your post method name is test which puts the table html in a div in index.html.erb
$('#report_table').html("<%= escape_javascript(render partial: 'report_table') %>");
add a div with report_table id in index.html.erb after the form
<div id="report_table"></div>
Add remote true in the form
form_tag(reports_test_path,:method => "post",:remote=>true)
add return in the test method which will render the required js file
respond_to do |format|
format.js
end
That should do it. Please let me know if you have any connfusion
You can submit the form by ajax by setting remote: true on the form tag:
<%= form_tag(reports_test_path,:method => "post", remote: true) do %>
<%= label_tag(:report_date,"Choose a Date") %>
<%= date_field_tag(:report_date)%>
<%= button_tag "Submit"%>
<% end %>
You then create a corresponding `app/views/reports/test.js.erb view file that generates the actual JavaScript code that will be sent and executed on the client side.
You can put the table in a partial, say app/views/reports/_table.html.erb and render it in the javascript:
var tableContainer = document.getElementById(...);
tableContainer.insertAdjacentHTML('beforeend', '<%= j render partial: "/reports/table" %>');
If you're not too familiar working with ajax and javascript, check out the Ruby on Rails guides
You can do this with server-side rendering
Add a template file with the following content:
views/reports/test.html.erb
<h1>Reports</h1>
<%# The date field from the index.html %>
<%= form_tag(reports_test_path,:method => "post") do %>
<%= label_tag(:report_date,"Choose a Date") %>
<%= date_field_tag(:report_date)%>
<%= button_tag "Submit"%>
<% end %>
<%# the new content %>
<table>
<th>
<td>Incoming Messages</td>
<td>Total Chats</td>
<td>Enrollment By Andi</td>
<td>Enrollment By Agent</td>
<td>Sent By Andi</td>
<td>Sent By Agents</td>
<td>Unread Messages</td>
</th>
<tr>
<td><%= #incoming_messages %></td>
<td><%= #total_chats %></td>
<td><%= #enrollment_by_andi %></td>
<td><%= #enrollment_by_agent %></td>
<td><%= #sent_by_andi %></td>
<td><%= #sent_by_agents %></td>
<td><%= #unread_messages %></td>
</tr>
</table>
You could reduce the repetition later on with partials and such, but that is not so important right now.
The more important problem is that your queries at least look you could do SQL injection with them (google for blog posts about "SQL injection active record rails"). Familiarize yourself with the ActiveRecord guides a bit more, things are more safe when you do it like this:
#chosen_date = Date.parse params[:report_date]
#incoming_messages = Message.where(inbound: 't').where(created_at: #chosen_date).count
Plus you don't need to write the SQL on your own.
Chaining the .where() leads to an AND in SQL and passing Date and Time objects (and Ranges) into the where() methods are improvements in the Rails releases from the later years
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..
I want to display only a single row in the table. My controller for the same looks like this:
def Add
#item= Item.find(params[:id])
end
and my table row looks like this.. Isn't there a substitute for the method .each to just display one record?
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
In this case you don't need loop. U can directly create table like this
<tr>
<td><%= #item.name %></td>
<td><%= #item.price %></td>
<td><%= #item.quantity %></td>
<td><%= #item.quantity * #item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
#item= Item.find(params[:id])
Here, #item will be an object of class Item. So You don't need to Iterate the Object with 'each'. You can directly access the Item attribute by #item object.
#item.name
#item.price
#item.quantity.....
I have the following HTML in my index file.
<% #posts.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
I have a function called is_eligible which takes post.team_name as an argument. I want to run it each "do iteration" and skip all three printing steps if it returns false
I'm not sure how to
include the file is_eligible is in and
structure the code within the html somewhere.
Any ideas?
# The view
<% #posts.each_with_index do |post, index| %>
<% unless post.is_eligible? %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
<% end %>
# The model post.rb
def is_eligible?
# and there you use self.name to access the name
end
<% #posts.select{|p| is_eligible(p. team_name)}.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>