Rails 3.2 using content_tag to generate a 'Delete' button with twitter-bootstrap icons - ruby-on-rails-3

I'm trying to replicate the Delete button icon in this example using the Rails 3 content_tag method, within a nested form and using jQuery unobtrusively (or at least I hope to be).
Twitter-Bootstrap Delete Button icon (example)
The html produced in when inspecting with Firebug is below.
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
I'm using the following to generate the button with an icon but cannot add the "Delete Ingredients" to it, nor do I get the "#" for the href.
Here is my code from within the ingredients partial:
<%= link_to content_tag(:a, content_tag(:i, '', class: "icon-trash icon-white"), class: "btn btn-danger remove_fields") %>
This generates:
<a class="btn btn-danger remove_fields">
<i class=icon-trash icon-white"></i>
</a>
This was based on information from
Api dock - content_tag
which had the following code example:
content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# => <div class="strong"><p>Hello world!</p></div>
Can someone kindly point me in the right direction? Why am I missing details I mentioned above?
N.B. I can get this to work with a link_to block but wanted to know if this can be done in one line without the do..end and more importantly in a content_for method.
<%= link_to('#', class: "btn btn-danger remove_fields") do %>
<i class: "icon-trash icon-white"></i>
Delete
<% end %>

<%= link_to(body, url, html_options = {}) %>
So your request in 1 line is:
<%= link_to content_tag(:i, "", class: "icon-trash icon-white") + "Delete", path_to_stuff, method: :delete, class: "btn btn-danger remove_fields" %>
The most complicated part is the 'body'. You just have to remember that all these content_tag helpers (including link_to and others), return a string.
BUT, this is ugly. AND long. AND hard to maintain. So your proposed solution that takes the block is much better.

why not just make a view helper method
def link_to_delete
link_to %{<i class="icon-trash icon-white"></i> Delete}, '#', class: "btn btn-danger remove_fields"
end
then call it in you views with link_to_delete
although why do you need your it to be a link
def delete_button
%{<span class="btn btn-danger remove_fields"><i class="icon-trash icon-white"></i> Delete</span>}
end
i dont like it as much with content_for but:
content_tag(:span, %{#{content_tag(:i, nil, :class => "icon-trash icon-white")} Delete}.html_safe, :class => "btn btn-danger remove_fields")
if you want it with an a tag just change :span to :a

Related

haml pull-right as a second class not working when followed by an if

So I have this piece of .erb code I want to translate to .haml. (some says haml is faster, I think i'm still strugling within the learning curve ;) )
<div class="row">
<div class="col-md-12">
<ul class="list-inline pull-right">
<% if current_page?(webservices_path) %>
<li class="submenu_links_current">
<% else %>
<li class="submenu_links">
<% end %>
<%= link_to webservices_path do %>
<i class="fa fa-home" aria-hidden="true" style="color: white;"></i>
Webservices
<% end %>
</li>
</ul>
</div>
</div>
The nearest haml translation I have atm :
.row
.col-md-12
%ul.list-inline.pull-right
- if current_page?(webservices_path)
%li.submenu_links_current
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
- else
%li.submenu_links
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
I have two problems with this haml right now.
.pull-right is not applied to ul if there's an if statement between ul and li.
I also found that this following more factorized code doesn't work since while currently on webservices_path, link_to webservices_path is not displayed.
.row
.col-md-12
%ul.list-inline.pull-right
- if current_page?(webservices_path)
%li.submenu_links_current
- else
%li.submenu_links
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
Thanks !
I think you want something like this:
.row
.col-md-12
%ul.list-inline.pull-right
%li{:class => (current_page?(webservices_path) ? 'submenu_links_current' : 'submenu_links')}
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
Note the two li have been combined into a single element, setting the class using the class element of the hash rather than the dot syntax. This removes the duplication and of the link_to. Also it is indented under the ul (in your code the if statements are at the same level, so the lis won’t be children of it).

Custom rails routes not routing like I expect

I have a jquery that everyone at my site can click on to update their subscription that looks like this
<div id="role-options-<%= current_user.id %>" class="modal modalSubscription" style="display: none;">
<%= simple_form_for current_user, :url => user_path(current_user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
...Form stuff
<div class="modal-footer">
<%= f.submit "Update Settings", :class => "btn btn-green" %>
<a class="btn btn-green closeModal" data-dismiss="modal" href="#">Close</a>
</div>
<% end %>
</div>
And i am trying to push this to a custom route that is an update subscription path. I am using devise. In a perfect world, i would like to send it to a separate controller that is labeled SubscriptionsController so that it can handle the update action. I've tried many things in the routes
#I expected this to send the request to the subscription controller after i changed the :url to update_subscriptions_path, but it did not
put "/users/update_subscription", :to => 'subscription#update'
I also tried just adding an "update_subscription" method in the controller and set the url in the form to
users_update_subscription_path
However this posted to the update path
Alright so was making a mistake, the paths i was adding were after resources :users, it looks like rails sees the put request and automatically assumes that I want to update the user and didn't make it down to the extra route I had added.

link to the current_user via href

Currently I have a header that links the user to his profile through this:
<li><%= link_to '<i class="icon-picture"></i> My Profile'.html_safe, current_user %></li>
I would like to be able to link the current user to his profile by doing something like this:
<li>
<a href="<%= current_user %>">
<div><%= image_tag current_user.avatar (:small) %></div>
<strong>Surge Pedroza</strong><br/>
view my profile page
</a>
</li>
But I do not know how to do it through an <a href="...">. Can someone please help me out?
This would be a better way to accomplish what you want:
<%= link_to current_user do %>
<div><%= image_tag current_user.avatar(:small) %></div>
<strong><%= current_user.full_name %></strong><br/>
view my profile page
<% end %>
link_to .. do is useful for the exact situation you are describing. Check the documentation for more examples.
I actually got it :)
<a href="<%= current_user.id %>">

RoR Difficulty accessing params in controller

I have an edit page with the following code, basically a dropdown and button that calls update.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label>Car Name:</label>
<%= select("params", ":id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
The server reads like the params is being set:
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"8vHXrnICaOKrGns6FfMUcd/dWo5kpNKpA8F5l5ozRkY=", "params"=>{":id"=>"9"}, "commit"=>"Switch Car"}
However, when I put the params into a session I get nothing. It seems to be always nil. Not sure what I am doing wrong? Here is code in the controller.
def update
if params[:id]
session[:car_info_id] = params[:id]
redirect_to entry_url
else
redirect_to switch_car_path
end
end
It always gets redirected to the switch_car_path so I am assuming params[:id] is always nil. When I put if params[:id] == nil it goes to the entry_url.
Thanks in advance.
you want params[:params][":id"]
Alternatively, you could put this in your view:<%= select("car_info", "id", #available_cars.collect {|v| [v.name, v.id]})%>
And then in your controller:if params[:car_info][:id]
While the other answer would work, this is probably what you'd want to be doing (using select_tag(:id) will automatically add an :id key/value to the params hash):
<% form_tag('switch_car', :method => :put) do %>
<div class="field">
<label>Car Name:</label>
<%= select_tag(:id, options_from_collection_for_select(#available_cars, "id", "name")) %>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
Then you can easily access params[:id] in the controller.

Render partial with content input in rails 3

I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>