Chart kick Timeline using Ruby on Rails and dynamic data - ruby-on-rails

I've installed Chartkick within my ROR app and I'm looking to use the timeline feature to display projects to give a simple and quick overview.
<%= #projects.each do |project|
timeline [
['project.hospital', "project.construction_start", "project.construction_end"],
] %>
<% end %>
I'm trying to get it to display all projects with the construction_start and end format be set at YYYY-MM-DD
I have no idea what to do... anything assistance would be appreciated

Your code is showing something you dont want, the loop instead the timeline, and it's showing , not one timeline but many of them .
Change your code to:
<% data = #projects.pluck(:hospital,:construction_start,:construction_end) %>
<%= timeline data %>
This should only create one timeline with all the data about the hospitals of all projects you passed from the controller .
Note that this will only work on Rails 4+. If you have Rails 3 you will have to override the pluck method (it's not hard actually)

Related

Hi, i am quite new to web developing. So forgive me for any stupid question

I am trying to build an ecommerce page using rails. In my products page I want to add products with an image, description, color and price. Everything works properly except for the images. I am not able to assign proper image to the desired product. If i use this syntax : <%= image_tag("/assets/6.jpeg", class: "img-fluid") %>, then this particular image is assigned to all other products and it's definitely not logical!
I have already added the images which I need for my project in the app/assets/images folder. I want to have the possibility to dynamically add/modify the photos on my page.
Please help me how to solve this issue. Thanking you guys in advance!
if images file are saved 1.jpeg-?.jpeg you could loop say
<% #product.each_with_index do |product, index| %>
<% i = index + 1 %>
<%= image_tag("assets/#{i}.jpeg"), class: 'image-fluid' %>
<% end %>
I mean for what you're asking this would work but is probably not the best way to go about this.

How display something if check box tag is checked?

Hello I’m beginner on ruby on rails and I have to build an application for my studies.
It will have users who can select differents activities and then have a list of all the things that they need for these activities ( For example if they select « Fishing » they will have a list as « bundles, fishing rod … » ).
I have a bidirectional has_and_belongs_to_many relationship between activities and things.
I would like to display all the activities and if the user selects some activities it should show things related to those activities.
How can I build such a view?
I try to put an « if check_box true ? » but it doesn’t work …. If you can help me, I would be grateful !
To display my list of activities I'm using this :
<%= form_tag do %> <% #activities.each do |activity| %> <li> <%= check_box_tag 'activity_ids[]', activity.id %> <%= activity.name %> <%= activity.content %> </li> <% end %>
Since this is for an assignment I will just try to help you along but not give you the whole answer as your teacher wants you to learn it.
I would use the form_for as described in: https://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
Then I would build the checkboxes using the collection_check_boxes method as shown here:
https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_check_boxes
It is unclear from your question but if the Things for each activity are already set, then you shouldn't need much more than this. If you try the methods above and have problems please edit your original question and I can try and help you further.
Try doing it via jQuery and ajax request. Create an action in a controller which retrieve data and call that action from the ajax request and then render the response data whereever you want.

Search Multiple models in ruby on rails

I am still a beginner in ruby on rails and I'm trying to create a simple search option for a website that I'm developing.
I have number of models such as Sundries, Curry and Starter.
My search works fine if I have one model but how do I search all models title or name?
I have added following method to each of the models and tried to display results in home page. but no luck!
def self.search(search)
where("LOWER(title) LIKE ?", "%#{search.downcase}%")
#where("content LIKE ?", "%#{search}%")
end
Can someone please help me figure this out with an example please?
Thank you.
You can add ActiveRecord_Relations together, and it will give you a simple array
#results = Sundries.search(search_term) + Curry.search(search_term) + Starter.search(search_term)
In the view...
<% #results.each do |result| %>
<%= "#{result.title} part of our fine #{result.class.name} choices" %>
<% end %>
Answering for anyone looking at this in 2019+... scenic is the gem you want to use (as long as your database is postgres).
There are several approaches how you can implement this. You can use advanced search engines such as ElasticSearch or Sphinx. Or you can use (for example) pg_search gem in case you are using Postgresql as your DataBase.
You can find RailsCasts for each of these approaches (some of them are probably for subscribers only).
As soon as you learning Rails, I would recommend to try out pg_search at first and avoid using search engines. Because it can be tricky sometimes for beginners. You can get to them later than you will have more data in your DB, or you would need to improve your skills. Try to make working solution in the first place and then improve it.
You can use this ransack to implement search functionality.
In your view side
<%= form_tag search_path, method: :get do %>
<%= text_field_tag :q, nil %>
<%= end %>
and in your controller action method,
q = params[:q]
#sunderies = Sundries.search(title_cont: q).result
#curry = Curry.search(title_cont: q).result
#starter = Starter.search(title_cont: q).result
#total_results = #sunderies + #curry + #starter

Presenting Information on home page from another model? Ruby on rails

I have a posts model... I am trying to display this model on the index page of the webpage.
<% #posts.each do |post| %>
<%= #posts[1].title %>
<%end%>
The problem with the above code is that it display the title 3 times because i have 3 posts. How do i edit this code to make it only display once.
You meant to do this:
<% #posts.each do |post| %>
<%= post.title %>
<%end%>
The each enumerator passes its variables one at a time into a block with the do |variable| syntax. So post is each item in #posts, one after another.
This is a really fundamental concept to Ruby and Rails: without understanding this further you probably won't get very far. I would recommend you go and read Programming Ruby -- a really excellent tutorial and reference to the Ruby programming language -- to understand how blocks and enumerators work.
If you use 'each' it displays all the posts because in your controller you had #posts = Posts.all. This gives all the posts written till now into the #posts instance variable and each enumerator prints all the posts
If you want only the title to be displayed and that too only for the first post, then you can write like this in your view
<%= #posts.first.title %>

Google-maps-for-Rails without a Model

Sorry to be newbie, but i wanna draw your attention to my problem while using gmaps4rails - great gem.
In my app there is no database backend. Actually i just want to collect markers like this:
<% markers=[]%>
<% #search.each do |dish| %>
<% if (not dish.restaurant.nil?)
restaurant=Restaurant.where( :id => dish.restaurant["Id"])
markers<< {:longitude => restaurant.lng , :latitude=> restaurant.lat}
end%>
<div id="dish_listed"><%= render 'dish_listed', :dish => dish %></div>
<% end %>
<%= gmaps4rails(markers.to_json) %>
Unfortunately it displays a gray rectangular whithout a map *(
I`ve managed to follow the steps from Wiki (i did it several times *))))
When i do
markers.to_json
i get
"[{\"longitude\":"30.252442359924316",\"latitude\":"59.92999013067258"}]"
And i have no idea why kind of code
<%= gmaps4rails('[{\"lng\":"30.252442359924316",\"lat\":"59.92999013067258"}]') %>
doesnt work? Where should it be initialized? Or what magic steps did i forget? I also forked a project by thasuresh. Started it and the removed everything from the model, and pushed my markers there, and it displayed me just what i wanted.
But i still cant reach the same result on the project i write by my self!!!
-----And now i`m sitting here and waiting for flying tomatos *)))-----
PS
rails 3.1rc5
gmaps4rails 0.10.2
BTW Could it be no gmappable model?
This works:
<%= gmaps4rails('[{"lng":"30.252442359924316","lat":"59.92999013067258"}]') %>
There are still little fixes to do for Rails 3.1, some threads are open on github.
To adjust the zoom level with one marker, check this question.

Resources