Nested url queries in ruby on rails - javascript

I'm making this choose your own adventure page and I'm using form_tag to accept user input. At the start of this page a user will enter a number through this text field
<% choice_1a = "1" %>
<% choice_1b = "2" %>
<%= form_tag("/pathfinder/quest1", method: "get") do %>
<center><%= text_field_tag(:a, #input_1, maxlength: 1) %>
<%= submit_tag("Submit") %></center>
<% end %>
<% input_1 = #input_1 %>
<% if input_1 == choice_1a %>
**Run code if user entered 1 **
<%= form_tag("/pathfinder/quest1", method: "get") do %>
<center><%= text_field_tag(:b, #input_2, maxlength: 1) %>
<%= submit_tag("Submit") %></center>
<% end %>
<% elsif input_1 == choice_1b %>
**Run code if user entered 2 **
<% else %>
** Prompt user to enter a 1 or 2**
<% end %>
Which results in this output in the url of the query "a" equaling whatever input was given. In this case I entered one ("&a=1")
https://192.168.0.200:3000/pathfinder/quest1?utf8=checkmark symbol&a=1&commit=Submit
After that path is taken they are given another form_tag that shown as below
Path if 1 was chosen at form_tag "a"
<%= form_tag("/pathfinder/quest1", method: "get") do %>
<center><%= text_field_tag(:b, #input_2, maxlength: 1) %>
<%= submit_tag("Submit") %></center>
<% end %>
When they enter a number here the url would change to this. Note how "a" was replaced with "b".
https://192.168.0.200:3000/pathfinder/quest1?utf8=checkmark symbol&b=1&commit=Submit
I was hoping to somehow nest these queries so that if a=1 I can have the user set the input to "b" it would still reflect their previous input to "a"
Here's a partial of the show section of the pathfinder controller that this code is running off
Controller code
if url == ('/pathfinder/quest1')
if params[:a].present?
#input_1 = "#{params[:a]}"
elsif params[:b].present?
#input_2 = "#{params[:b]}"
end
render 'quest1/index'
What I'm trying to do is give the user the option to set a=1 and then under that branch allow b to set 1 or 2. An example of what I'm trying to accomplish
a=1 (being one path) or a=2(being the other path)
| |
V V
b=1 <---> b=2 c=1 <-----> c=2
**Different paths if the user were to set "a" to one or two**
If the user set "a" equal to one, they would have the option of setting "b" to one or two. The same concept would be for "a" equal to two where the user would only have the option of setting "c" to one or two.
I'm open to any other ideas that doesn't require changing the url if its through html, css or javascript

Related

Set character limit in ejs

I am fetching content from JS in EJS template. I have a body field that contains more than 200 characters, but I want to show only 100 Characters in my view.
Simply you can put up an if else condition like this in your EJS Template:
<%if (yourField.length > 100) { %>
// Code to show 100 characters
<%= yourField.substring(0, 99) %>
<% } %>
<% else{ %>
//COde to print full value of your field
<%= yourField %>
<% } %>

Access form_group div via id

I want to perform a check to provide instant validation feedback on the input of a field in my bootstrap form for my Profiles model using java/coffee-script. My current solution is working but feels rather clunky and I would like to improve it.
Here are some code examples of what I'm doing.
app/views/profiles/_form:
<%= bootstrap_form_for(#profile, layout: :horizontal, html: {id: "profile-form"} ) do |f| %>
<%= f.text_field :name %>
<%= f.text_field :number, id: "number-field" %> //Field to check
<%= form_group do %>
<%= f.primary %>
<% end %>
<% end %>
app/assets/javascript/profile_number_control.coffee:
$(document).on 'ready page:load', ->
$field = $("#number-field")
$group = document.getElementById("profile-form").childNodes[5]
numberPattern = "^(19|20)[0-9]{6}-[0-9]{4}$"
$helpBlock = $group.getElementsByClassName("help-block")[0]
$field.keyup ->
//This works fine, just wanted to show what is going on
if inputIsValid()
set $group class to " has-success"
set $helpBlock message to successful
else
set $group class to " has-error"
set $helpBlock message to corresponding error message
Here is the problem. I want this script to work for both profile#new and profile#edit, but since the array of elements given by document.getElementById("profile-form").childNodes differs in length depending of the current view I can not use the same index (5 in the example) for both #new and #update.
Does anyone know how to set an id to that specific form_group in my view? Or do I have to actually write out the whole html code to be able to place an id there properly?
This might help !
<%= bootstrap_form_for(#profile, layout: :horizontal, html: {id: "profile-form"} ) do |f| %>
<%= f.text_field :name %>
<%= f.text_field :number, :input_html => { id: "number-field" } %> //Field to check
<%= form_group do %>
<%= f.primary %>
<% end %>
<% end %>

Rails - refreshing page with new table entries

The goal I am trying to achieve is to let the user click on a button which will refresh to page filter that search. My page is dedicated to music artists and would like that once the user selects "Pop", that the page refreshes showing only "pop" artists.
Here is the html + ruby that I am using to display the artists:
<% #prints the artists that match the search, prints all for empty search %>
<% #artists.each_slice(3) do |artists| %>
<div class="row">
<% artists.each do |artist| %>
<div class = "artist_images">
<%= link_to image_tag(artist.artist_art, class: 'show_image'), artist_path(artist) %><br/><br/>
</div>
<% end %>
</div>
<% end %>
<% #prints message showing that the search does not match %>
<% if !#artists.present? %>
<h3><center style = "color: white">There are no artists containing the term(s) "<%= params[:search] %>". Please try 'Adele', 'Drake', 'Kanye West', or 'Taylor Swift'</center></h3>
<% end %>
The controller has the following methods:
def index
#artists = Artist.all
end
def randomPop
#artists = Artist.where(:genre => "\nPop").random(9)
end
Does anyone know how I can go about changing the variable #artists from All artists to those in pop only through a button click?
in your view,
<%= button_to "Pop", artists_path, method: :get, params: { artist_genre: "Pop" } %>
in your controller,
def index
if params[:artist_genre]
#artists = Artist.where(:genre => params[:artist_genre]).random(9)
else
#artists = Artist.all
end
end
This classic railscast episode is on point. I would use that for how you set up your search box. The only thing you might alter/update:
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
Should probably be a where clause based on your situation"
if search
where("genre = ?", your_genre)
else
all
end

How to return a selected value from a database collection in Rails?

So I have a database collection labeled #joe, and I am trying to pass the id variable into a link url path. This is in my index.erb.html file.
<%= select_tag "article", options_from_collection_for_select(#joe, 'id', 'title'),
prompt: "Select Something" %>
In my articles_controller.rb file, I have
def index
#joe = Article.all
end
I made a loop where it grabs each id number and passes it into the article_url path. I was wondering, in the dropdown box that I created above, when I select a certain value, I want to pass that value into the article.id variable. I'm having trouble with this and it outputs x amount of buttons.
<% #joe.each do |article| %>
<%= button_to "SELECT", article_url(article.id), :method => 'get' %>
<% end %>
I was wondering if it is possible to do this or if I can implement JavaScript or jQuery into this. Any help would be appreciated. Thank you!
And here's the rake routes command results:
Prefix Verb URI Pattern Controller#Action
home_index GET /home/index(.:format) home#index
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / home#index
Let's try this:
ERB
<%= form_tag article_path do %>
<%= select_tag "id", options_from_collection_for_select(#joe, 'id', 'title'), prompt: "Select Something", class: 'select' %>
<%= button_tag 'SELECT', class: 'button' %>
<% end %>
JS
$('.button').on('click', function() {
var path = $('form').attr('action');
var articleId = $('.select').val();
$.get([path, articleId].join('/'));
});

Changing hyperlink on refresh

How to put just one random link(out of many) on page every time a user reloads the page?
e.g
link 1
link 2
...
or in rails
<%= link_to "link 1", "https://google.com" %>
...
just use some rand value, for example
/*view.html.erb*/
<% var ||= rand(2) %>
<%= link_to_if var == 0, "http://www.google.com" %>
<%= link_to_if var == 1, "http://www.facebook.com" %>
you can set this into a partial, or maybe an array in your controller a call the array with de rand value like
<%= link_to #array_of_links[var] %>
or if you want to save this into your db you can just pick a random record with
/*controller*/
#link = Link.order("RANDOM()").first
because of MVC flow it will get a random value each time, regards

Categories

Resources