Haml Bootstrap search form - ruby-on-rails-3

My navbar code looks like this:
%header.navbar.navbar-fixed-top
%div.navbar-inner
%div.container
%nav
%ul.nav.pull-right
- if user_signed_in?
%li= link_to "Sign out", '#'
- else
%li= link_to "Sign in", '#'
I am trying to get my navbar to look like that in the Bootstrap navbar page as shown here, however I'm not sure how to translate
<form class="navbar-search pull-left">
<input type="text" class="search-query" placeholder="Search">
</form>
into Haml. My last attempt look like:
%header.navbar.navbar-fixed-top
%div.navbar-inner
%div.container
%nav
%ul.nav.pull-right
%form.navbar-search
%input.search-query
- { :type => "text", :placeholder => "Search" }
- if user_signed_in?
%li= link_to "Sign out", '#'
- else
%li= link_to "Sign in", '#'
While the search bar appears with the proper CSS classes, the :type => "text" and :placeholder => "Search" portions do not exist in my html when I inspect it. How can I fix this? Thanks!
== EDIT ==
Perhaps to make this clearer - I'm using this in a rails 3 app and I realize that I should probably be using form_tag, but I'm not sure how to proceed.

It should work like this:
%header.navbar.navbar-fixed-top
%nav.navbar-inner
.container
%ul.nav
- if user_signed_in?
%li= link_to "Sign out", '#'
- else
%li= link_to "Sign in", '#'
= form_tag some_path, :class => "navbar-search pull-left", :method => :get do
= text_field_tag :name, nil, :class => "search-query span3", :placeholder => "Search"
Be careful with the indentation.
I also advise you to try to start with rails app template and tutorial: http://railsapps.github.com/. As for a beginner it would be an excellent example of what and how to set up and how it should look like.

Related

Use icomoon with link_to and icon_tag

I'm trying to move from Font Awesome to icomoon.
With Icomoon I can get the icons to work using the following syntax
<span data-icon="" aria-hidden="true"></span>Some Text
However, as I'm using a rails app I'd really prefer to use the following syntax, or something similar.
<%= link_to icon_tag("icon-pdf", "some text"), controller_path %>
I have tried the following as well, all to no avail
<%= link_to 'Some text', controller_path, {"data-icon" => "", "aria-hidden" => "true"} %>
It doesn't matter if I put the defined name (icon-pdf) or its hex value in there, but I can't seem to get the icon to appear.
Is there a way I can achieve this, or am I stuck with the data-icon method?
Try with this:
<%= link_to controller_path do %>
<span data-icon="" aria-hidden="true">Some Text</span>
<% end %>
Or
<%= link_to content_tag(:span, 'Some Text', :data_icon => "", :aria-hidden => "true" ), controller_path %>
It should work. Thanks

undefined method `model_name' in a partial rendered by different controller

I'm trying to render this form:
<form class="form-inline">
<%= simple_form_for #prospect,
:url => url_for(:action => 'create', :controller => 'prospects'),
:method => 'post' do |f| %>
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Name', label: false %>
<%= f.input :email, placeholder: 'Email', label: false %>
<%= f.input :interests, placeholder: 'Tell us what you were searching for', label: false, value: params[:search] %>
<%= f.error :base %>
<%= f.button :submit, "Submit", :class=> "btn" %>
<% end %>
Using this partial:
<%= render partial: 'prospects/novideo_capture' %>
The partial is in a view controlled by Videos#index controller, and I keep getting this error: 'undefined method `model_name' for NilClass:Class'
This is my prospects controller:
class ProspectsController < ApplicationController
def index
#prospects = Prospect.all
end
def new
#prospect = Prospect.new
end
def create
#prospect = Prospect.new(params[:prospect])
if #prospect.save
render "thanks_for_interest"
else
render "novideo_capture"
end
end
I'm not sure what I'm going wrong, although I'm pretty sure it's a simple solution. I've seen a lot of similar questions around SO and tried all their answers, but none of them seem to work for this situation.
Thanks for any help...
EDIT: Adding
#prospect = Prospect.new
to the videos index controller stops the error occurring, but I don't feel it's the right way to do this. It also doesn't actually make the form use the prospects controller.
EDIT2: I now have the partial rendering correctly (I think), and my videos#index calls the partial like this:
<%= render partial: 'prospects/novideo_capture', :prospect => #prospect %>
Then simple_form in the partial looks like this:
<form class="form-inline">
<%= simple_form_for :prospect,
:url => url_for(:action => 'create', :controller => 'prospects'),
:method => 'post' do |f| %>
...
<% end %>
However it's not actually submitting the form with the prospects controller. Any ideas why?
Check your markup. You're wrapping a simple_form inside another form. Since the first form tag has no action associated with it (<form class="form-inline">), that form will submit against the current URL, which is the video#index.
You're going to want something like this:
<%= simple_form_for :prospect, :url => etc, :method => 'post', :class => "form-inline" do |f|
...
<% end %>
Losing the leading (redundant) form-inline form tag and you'll be fine.

Is it possible to call (:subdomain => false ) on current_user?

In my header I have this:
<%= link_to "My profile", current_user %>
Is there anyway to call (:subdomain => false) or something similar on it so it will escape a subdomain when in it? I'm using devise.
It works perfectly in these instances:
<%= link_to 'About', about_url(:subdomain => false) %>
But because it doesn't have '_path' or '_url' in it, I keep getting a 'wrong number of arguments 1 for 0' error.
Thanks for any help...
You can call link_to with the user_url(current_user) helper instead of the current_user shortcut.
I guess this would work :
<%= link_to "My profile", user_url(current_user, :subdomain => false) %>.

Ruby on Rails - Multibutton combined with checkboxes and advanced search

Currently I have the situation that one button is add to the index-form. This workes perfectly in combination with the other functionalties as search and checkboxes which are also part of the index-form.
Code of the index.html.erb:
<%= form_tag order_path, :method => 'get' do %>
<p>
<%= submit_tag "Historical Search", :account_short => nil %>
<%= text_field_tag :search, params[:search] %>
</p>
<% end %>
<%= form_tag sendnext_order_path, :method => :put do %>
<%= submit_tag "Send to Desk" %><br/>
-- other code from index-form
<% end %>
Combined with the controller:
def sendnext
Order.update_all(["status_id = ? ", "2"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order succesfully send to desk.'
end
Now I want to add a second button next to the Send to Desk button with another action than the excisting working one. Until now I'm not capable to realise this.
Please advice. Any feedback is welcome.
Use button_to
<%= button_to('Send to Desk', 'sendnext', :method => "put") %>
<%= button_to('Cancel Order', 'cancelorder ', :method => "put") %>
Will take care of the form for you. submit_tag is submitting the first form I believe.
For example
<%= button_to "New", :action => "new" %>
will generate
# => "<form method="post" action="/controller/new" class="button_to">
# <div><input value="New" type="submit" /></div>
# </form>"
Thanks for your help. I have found a working solution which realised my current requirements:
The index.html.erb looks like the following:
<%= form_tag updateorder_order_path, :method => :put do %>
<%= submit_tag "To Desk" %><br/>
<%= submit_tag "Cancel Order" %>
-- other code like data fields
<%end %>
The controller.rb looks like the following:
def updateorder
if params[:commit] == "To Desk"
Order.update_all(["status_id = ? ", "2"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order(s) successfully send to desk.'
elsif params[:commit] == "Cancel Order"
Order.update_all(["status_id = ? ", "3"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order(s) successfully cancelled.'
else
Order.update_all(["status_id = ? ", "5"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order(s) successfully updated.'
end
end
The routes.rb contains the next code:
resources :orders do
put 'updateorder', :on => :collection
end

rails 3 link_to with nested content_tag to create <a href with nested span - how to?

Hi I got a noob question, I want to create the following HTML result:
TEXT<span class="arrow-big"></span>
In the above HTML I want to have text with a span-class to style in an image via css.
When I try the following implementations the result reflects just one part of the needed implementation:
<%= link_to "TEXT", controller_path, :class => "button-big layer" %>
results in:
TEXT
and
<%= link_to(content_tag(:span, "", :class => "arrow-big"), controller_path, :class => "button-big layer") %>
results in:
<span class="arrow-big"></span>
Does anyone know how to accomplish?
You could also nest tags by using alternative syntax for link_to helper
<%= link_to controller_path, :class=> "button-big layer" do %>
Text
<%= content_tag(:span, "", :class => "arrow_big" %>
<% end %>
Simply concatenate your text with the 'span':
<%= link_to(("TEXT" + content_tag(:span, "", :class => "arrow-big")).html_safe,
controller_path,
:class => "button-big layer") %>
You'll need the .html_safe around the concatenation since the + operator will otherwise escape the HTML of the content_tag.
Reading your question I did solve my problem.
Than I propose another way to answer your question.
You could create a helper method to make this kind of link that you need.
It would be something like this
def link_with_text_and_span(href, text, span_options= {}, link_options = {})
span_tag = content_tag(:span, span_options[:content] || '', :class => span_options[:class] || '')
link_to(span_tag, href, :class => link_options[:class] || '')
end
The good about it is that your view will be cleaner.
Then you can just call this helper method in your view
<%= link_with_text_and_span("/controller/action", "TEXT", {class: 'arrow-big'}, class: button-big) %>
PS: This code can be improved for sure, if other users want to, please do it.
Here's another way you could use without the content_tag. Not the cleanest but it works!
<%= link_to '<span class="arrow_big"></span>'.html_safe, controller_path, class: "button-big layer" %>