Rails 4 - How to save selections from different dropdown menus in one view and passing them to controller to receive corresponding data - ruby-on-rails

I'm very new to Rails and have just started to create my first app. this question may sound a little weird.
So i have two drop down menus, Year and Region, they each have some selections. i have an articles controller and model to store some info. I'm trying to make this action work:
first make a selection from "Year", say 2014. As soon as i click on 2014 i want to display all the articles from the database with the year 2014
then, while the "Year" dropdown menu is still in 2014, I make a selection in the "Region" dropdown menu, say US, and have the view display articles that belongs to 2014 and US.
is it possible to do all the above without using ajax?
here's my view code for articles/index
<!-- Region -->
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Region
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><%= link_to "All Regions", ?????_article_path(???) %></li>
<% #articles.each do |article| %>
<li>
<%= link_to article.region, ?????_article_path(???)%>
</li>
<%end%>
</ul>
</div>
<!-- Years -->
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Year
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><%= link_to "All Years", ?????_article_path(???) %></li>
<% #articles.each do |article| %>
<li>
<%= link_to article.year, ?????_article_path(???)%>
</li>
<%end%>
</ul>
</div>
here's my ArticlesController, it just have one index method
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
So i thought about putting another method in there called selection but i'm not sure how to pass the second dropdown menu's selection.

If you want to actively get the articles from the database, you need AJAX. In case you are OK with loading the articles together with the view, you can simply store them as a pre-fetched JSON object like this:
<script>
var articles = <%= raw #articles.to_json %>;alert(articles);
</script>
And then either manually go through this JS object.
Tip: to have more control over displaying and rendering of JS objects, install backbone.js (http://backbonejs.org/), install backbone.js query (https://github.com/davidgtonge/backbone_query) make a articles Backbone model (tutorials on backbone.js page are really simple and helpful), articles_collection Backbone collection, query it the way you want, and then make HTML code via Javascript and paste it on the page. I prefer using Backbone.js underscore.js templates for that, they are an excellent tool to master.

Related

Open a modal in rails

I am very new for rails and I try to open an modal in my app. Here is my show.html.erb
app > views> event > show.html.erb
<div>
<button class="btn btn-info slide-down-right-drawer-btn" data-slide-block-id="#discussion-drawer">
<i class='ion ion-chatbubbles icon mr-5'></i>Discuss
</button>
</div>
This is my app > views > event > _discussion_drawer.html.erb
<div id="discussion-drawer">
<div class="col-md-12 col-sm-12 col-xs-12" style="line-height: 50px;">
<ol class="breadcrumb">
<li>Discussions</li>
<li class="active"><%= #co["name"] %></li>
</ol>
<button type="button" data-toggle="modal" data-target="#discussionFormModal" data-entity-id="<%= #co["id"] %>" class="btn btn-conf add-note-btn btn-info mb-20" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<b>Start new discussion...</b>
</button>
</div>
</div>
But this is not work. Am I doing right? Can any one help me for this?
It's not super clear which is the modal you're trying to open and where is the button to toggle it, but it took me a few tries to get comfortable with this too.
Make sure the ID on your modal matches the ID on the button
# this is on your button
data-toggle="modal" data-target="#discussionFormModal"
# this is on the modal
id="discussionFormModal"
Check that you've rendered the partial (_whatever_the_name_is.html.erb) properly in the same page that the button to open the modal is
<%= render partial: "<path to your partial>", locals: {<any variables that you need to pass to the modal>} %>
#example
<%= render partial: "shared/discussion_form", locals: {discussion: #discussion} %>
I think your HTML code is correct but bootstrap js is not loaded properly. you can simply check using browser inspect.
Make sure that your bootstrap.min.js loaded properly in rails app. you can also use CDN https://getbootstrap.com/docs/4.5/getting-started/download/
or
If you are using bootstrap gem follow https://robrace.dev/integrating-bootstrap-4-in-rails-6/ this article

How to build out custom view helpers for Rails with an onclick and inline styling?

I'm working on restructuring my code to clean it up and I'm trying to move over into using view helpers to do this. Right now I have the following in my views file:
<div class="btn-group wkt-btn-group">
<button type="button" class="btn share dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">SHARE
<span class="fa fa-angle-down" style="font-size:16px;"></span>
</button>
<ul class="dropdown-menu wk-social">
<li>
<div class="jssocials-share jssocials-share-email">
Print
<i class="jssocials-share-logo"></i>
</div>
</li>
<li>
<div class="jssocials-share jssocials-share-email">
<a href="mailto:?Subject=Book Subject&Body=I%20saw%20this%20and%20thought%20of%20you!%20 http://url.com/books/<%= book.book_name %>" class="jssocials-share-link">
<i class="fa fa-at jssocials-share-logo"></i><span class="jssocials-share-label">E-mail</span>
</a>
</div>
</li>
<li>
<div class="jssocials-share jssocials-share-facebook fb-share-button" id="fbid">
<div data-href="http://url.com/books/<%= book.book_name %>" data-layout="button" data-size="small" data-mobile-iframe="true">
<a class="fb-xfbml-parse-ignore jssocials-share-link" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.url.com%2Fsomebooks%2Fbooks%2F<%= book.book_name%>&src=sdkpreparse">
<i class="fa fa-facebook jssocials-share-logo"></i><span class="jssocials-share-label">Share</span>
</a>
</div>
</div>
</li>
The only thing I can think of to alter this to a view helper is to go to books_helper.rb and do something like:
module BooksHelper
def social_sharing (media)
when 'email'
content_tag(:div, class: 'jssocials-share jssocials-share-email')
end
end
I've barely scratched the service here but I feel as even that is incorrect. I've nothing that's looking for the media type so email is worthless. Plus there's a div class around the href I'm hitting but the href has it's own class and an onclick plus styling. Any nudge would be appreciated. I have never built out a custom view helper.
You could pass a block...
<%= social_sharing 'email' do %>
Print
<i class="jssocials-share-logo"></i>
<% end %>
And define your helper method as...
module BooksHelper
def social_sharing(media, &block)
case media
when 'email'
content_tag :div, class: 'jssocials-share jssocials-share-email', &block
end
end
But I think that might just make your code more obscure even if it does DRY up the jssocials classed divs. I highly recommend "99 Bottles" by Sandi Metz which amongst other things talks about producing clear, maintainable code.
https://www.sandimetz.com/99bottles/

Bootstrap 3: how to link directly to a tab from another page in Rails 4

I am building a web app with Rails 4 and Bootstrap 3.
In one of the pages, I have the following Bootstrap tabs:
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Profile</li>
<li role="presentation"><span class="glyphicon glyphicon-credit-card" aria-hidden="true"></span> Billing</li>
<li role="presentation"><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> Expert</li>
</ul>
and then, the following content in these tabs:
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="profile">
</div>
<div role="tabpanel" class="tab-pane" id="billing">
</div>
<div role="tabpanel" class="tab-pane" id="expert">
</div>
</div>
When I hover any of these tabs, I see that the direct URL is, ie: http://localhost:3000/account/edit#profile, http://localhost:3000/account/edit#billing and http://localhost:3000/account/edit#expert.
However, when I try to link directly to the tab from another page, the active tab is still the first one (not the one I linked to).
Two notes:
I am using Bootstrap tabs "without any JavaScript", as explained in the markdown section in the documentation.
I would like to link to these tabs using Rails link_to helper.
Any idea why this is not working?
I don't think you'll be able to accomplish what you're after without throwing some javascript into the mix, as the tabs are meant for intrapage nagivation, not interpage navigation. Additionally, the href tags aren't even required on those tabs, as it's the data-toggle attribute which controls which tab pane to present.
If adding a small javascript snippet is a viable option, this will switch to the appropriate tab when the page is navigated to.
hash = window.location.hash
$("a[href=#{hash}]").click()
https://jsfiddle.net/tL7sutnt/
You're setting the active tab in the view
<li role="presentation" class="active"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Profile</li>
which is going to take precedence.
If you want this to work, you need to add the active class to whatever tab is being referenced in the url anchor (#expert, in your case)
See this post if you need code reference.
***EDIT:
Since you aren't using js, use the answer at the bottom that dynamically determines the active tab by parsing the request

Drop Down List With Search Button In Rails

I have a customer model with the following collection:
#doctors = Customer.joins(:user).where(users: {role: User.roles[:doctor]})
which joins the user table and customer tables where user role is "doctor".
In my Customers index view currently I am listing all the customers, now I want to add a search filter using a Drop down list with a search button, where the list contains the first name of the Doctors. If I press the search button it should show only customers created by that particular doctor selected from the drop down list.
1. How to add a drop down list with search button which displays only first names of doctors ?
2. How to filter my customers listing when I select a particular doctor from the drop down and click on search button ?
<div class="btn-group">
<button type="button" class="btn btn-success btn-sm"><b>Search By Dr.</b></button>
<button type="button" class="btn btn-success btn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li>List All Customers</li>
<% #practices.each do |practice| %>
<li>Dr.<%= practice.firstname %></li>
<% end %>
</ul>
</div>
The above is the code that I am currently using, but is it correct way of Rails ?
Answer 1:
you can do this task using JS I will recommend you to use one of these jquery plugins for search bars Select2 and Chosen.
My personal experience with select2 is impressive.
it loads data via ajax and cache as well to avoid repeat calls. Its GUI give u a Google like feel. read its documentation and use it.
Answer 2:
select2 also give u events like change so u can make another ajax call to load filterd customers.
You can simply use the following tag in your rails view:
<%= select("doctor", "firstname", #doctors.all.collect(&:firstname)) %>
Then in your controller you'd have something like this:
def index
if params[:doctor]
#customers = Customer.joins(:user).where(users: {role: User.roles[:doctor]}, firstname: params[:doctor][:firstname].downcase)
else
#customers = Customer.all
end
end
You can do this auto_complete rails. Have a look on
http://railscasts.com/episodes/102-auto-complete-association
Thanks

Change <div> innerText from NestedMasted Page so that the Changed text remains throughout session

I want to change the inner text value of a div that is located on my site.master page, and I want the changed value to remain for the rest of the session.
The value I want to pull through is the User name that is obtained from OAuth
By Using: User.Identity.Name;
I can only access this property once a user has authenticated, based on my redirect after authentication the page then moves to a nested master page for the members_only section.
I can change the text of the div with this code:
((HtmlGenericControl)Master.Master.FindControl("LoginButton")).InnerText = User.Identity.Name;
But as soon as I redirect, the text changes back to null.
I use it for my bootstrap user button which looks like this
<div class="btn-group">
<a class="btn btn-primary" href="#"><i class="icon-user icon-white"></i> <div id="LoginButton" runat="server"></div></a><a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a><ul class="dropdown-menu">
<li><i class="icon-pencil"></i> Manage</li>
<li class="divider"></li>
<li><i class="i"></i>Logout</li>
</ul>
</div>
Any advice would be greatly appreciated.
You could just use the LoginName control to do all that for you - just drop it into your MasterPage.

Resources