Search field doesn't seem to appear in view - ruby-on-rails

I have the following code in my view file
<%= form_tag search_path, class: "form-inline", method: :get do %>
<div class="input-group input-group-lg">
<% if params[:query].present? %>
<div class="input-group-btn">
<%= link_to "clear", search_path, class: "btn btn-default" %></div>
<%= text_field_tag :query, params[:query], id: "univ_search", autocomplete: "on"%>
<div class="input-group-btn">
<%= submit_tag "Search", class: "btn btn-primary"%>
</div>
</div>
<%end%>
<%end%>
However, it doesn't show any text field or buttons in the browser. Can someone suggest a way to resolve this?

Looks like the condition
<% if params[:query].present? %>
is preventing the view.

Related

Rails - Unable to visit link

I tried to follow a tutorial by Mackenziechild.me.
Below is the code in github https://github.com/ikanyu/raddit
Demo app: https://whispering-shelf-9164.herokuapp.com/links/1
Can anyone point out why when I inspect element on the link I have created(for example Facebook link), I can clearly see facebook. However, when I click on the link, it gives me error.
ActiveRecord::RecordNotFound in LinksController#show
Couldn't find Link with 'id'=facebook
def set_link
#link = Link.find(params[:id])
end
def authorized_user
...
Thanks!!
<div class="page-header">
<h1><%= #link.title %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>
<div class="btn-group">
<%= link_to 'Visit URL', #link.url, class: "btn btn-primary" %>
</div>
<div class="btn-group pull-right">
<%= link_to like_link_path(#link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= #link.get_upvotes.size %>
<% end %>
<%= link_to dislike_link_path(#link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down">
Downvote
<%= #link.get_downvotes.size %>
<% end %>
</div>
<% if #link.user == current_user -%>
<div class="btn-group">
<%= link_to 'Edit', edit_link_path(#link), class: "btn btn-default" %>
<%= link_to 'Destroy', #link, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default" %>
</div>
<% end %>
<h3 class="comments_title">
<%= #link.comments.count %> Comments
</h3>
<div id="comments">
<%= render :partial => #link.comments %>
</div>
<%= simple_form_for [#link, Comment.new] do |f| %>
<div class="field">
<%= f.text_area :body, class: "form-control" %>
</div>
<br>
<%= f.submit "Add Comment", class: "btn btn-primary" %>
<% end %>
If you're using the following html to create a link in rails
Facebook.com
Rails will link you to YourApp.com/facebook.com (which is an invalid URL).
Try re-writing your link like this:
Facebook.com
You'll need to change the code in your view to this:
<div class="page-header">
<h1><%= #link.title %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>
Alternatively, you can edit your #link.url to include the string "http://www."

In Ruby on Rails, how to customize the submit button to use BootStrap and add a icon to it?

In BootStrap, I can use the following code to get a button with an icon.
<button class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span> Click Me</button>
Now I want to do the same thing for the submit button of a Rails form. I don't know how to do it and below is my code:
<h1>Create a post</h1>
<%= form_for :post, url: posts_path do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
<p class="help-block">Please type the title of the post</>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, class: "form-control", rows: 5 %>
<p class="help-block">Please type the body of the post</>
</div>
<%= f.submit class: "btn btn-primary" %>
<% end %>
Could someone give me any suggestions?
I found the solution here: HTML code inside buttons with simple_form
<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
Text
<% end %>

How to perform a query via bootstrap search box

In the form_tags I can easily add methods and get params from the text_field_tag, for example to query a database or to perform a search.
FORM_TAG
</form>
<%= form_tag usernotes_path, method: 'get' do %>
<%= text_field_tag :query, params[:query], size: 150, placeholder: "Search", class: "searchInput"%>
<%= submit_tag 'Search', class: 'btn btn-success' %>
<% end %>
</div>
Now I am trying to use bootstrap "search" form, but not sure how to achieve the same result as the form_tag (where to put the method, how to add actions, how to perform a query?)
BootStrap Search
<div class="col-sm-3 col-md-3">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
<div class="col-sm-3 col-md-3">
<%= form_tag usernotes_path, method: 'get', class: 'navbar-form', role: 'search' do %>
<div class="input-group">
<%= text_field_tag :query, params[:query], size: 150, placeholder: "Search", class: "form-control"%>
</div>
<%= submit_tag 'Search', class: 'btn btn-success' %>
<div class="input-group-btn">
<%= button_tag(type: "submit", class: "btn btn-default") do %>
<i class="glyphicon glyphicon-search"></i>
<% end %>
</div>
<% end %>
</div>
This should be it, didn't tested it in an app but I think it will work, let me know in a comment if it's not.

text_field_tag doesn't show any field in my view

Here is my view file
<%= form_tag results_path, class: "form-inline", :method => :get do %>
<div class="input-group input-group-lg" >
<div class="input-group-btn">
<%= link_to "clear", results_path, class: "btn btn-default" %>
</div>
<%= text_field_tag :query, params[:query], class: "form-control", id: "result_search", autocomplete: "on" %>
<div class=input-group-btn">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
</div>
<%end%>
But I can see only clear button. Both text field and submit button are not displayed and when I hover around I see a disable symbol. Can anyone tell me where I am doing this wrong?

Submit button too large

i play around with Ruby on Rails in combination with Bootstrap.
What i have done is a form for contact.
My problem is when i create the form with ruby-syntax like:
<%= form_for #contact, html: {role: "form"} do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, :class => "form-control" %>
</div>
<div class="form-group">
<%= f.label :message %>
<%= f.text_field :message, :class => "form-control" %>
</div>
<%= f.submit "Send", class: "btn btn-primary" %>
<% end %>
then the submit-button gets very large (100% of the page).
In the custom Bootstrap they take the button-tag and not the submit-tag.
Is their any possibility to implement a button-tag in Ruby on Rails as a submit-button?
Thanks
Is their any possibility to implement a button-tag in Ruby on Rails as
a submit-button?
There is a button_tag for you.It just as like a HTML submit button.
<%= button_tag "Send", class: "btn btn-primary" %>
#=><button name="button" type="submit" class ="btn btn-primary">Send</button>
For more details, see this API
Ofcourse,you can do it like this too
<%= f.button "Send", class: "btn btn-primary" %>

Resources