Rails and WiceGrid : HTML escape - html

I would know if it is possible to do something like this with a Rails wice grid plugin:
<g.column do |model| %>
<ul class='list-inline'>
<li>
<%= link_to model_path( model ), :title => 'See' do %>
<i class="glyphicon glyphicon-eye-open"></i>
<% end %>
</li>
<!-- ... -->
</ul>
< end -%>
Is it possible for a wice grid column to contain an HTML <ul> tag?

I was searching for the same but finally figured it out. So adding this as an answer though its too late but might help someone else.
I think we can't use ERB in the grid column but we can use Ruby/Rails code in there.
I think we can use HTML like this but have to use html_safe option
"<ul class='list-inline'><li><a href='#'><i class='fa fa-eye'></i></a></li></ul>".html_safe
But i preferred this to generate the HTML.
g.column do |model|
content_tag(:ul, class: 'list-inline') do
concat(content_tag(:li, link_to('<i class="fa fa-eye"></i>'.html_safe, controller_path(model), title: 'See')))
concat(content_tag(:li, link_to('<i class="fa fa-wrench"></i>'.html_safe, edit_controller_path(model))))
end
end

Related

Links in navbar with Rails and Bootstrap

Say I have a navbar:
<ul class="nav navbar-nav">
<li><%= link_to 'Item 1', item1_path %>
<li><%= link_to 'Item 2', item2_path %>
...
But this way current menu item is also a link. How do I make it into a span, preserving appearance? Which doesn't look like a good options, since in this case I need to duplicate bootstrap's styles. Or make it look like a menu item without it pointing to any page?
A link_to method will always render an <a> tag, but you can render a <span> inside of the <a> tag if you use link_to ... do
This is an example taken from this documentation
<%= link_to(#profile) do %>
<strong><%= #profile.name %></strong> -- <span>Check it out!</span>
<% end %>
<!-- which renders -->
<a href="/profiles/1">
<strong>David</strong> -- <span>Check it out!</span>
</a>
I came up with the following helper:
module ApplicationHelper
def menu_item(name, path)
if current_page? path
content_tag('a', name)
else
link_to name, path
end
end
end
With that we've just got to replace link_to with menu_item in original code.

Using ROR instance variable in form tag

I am working in ROR(ruby on rails) and I am trying to use a ror variable in form tag, but getting error
here is my code
#answers.each do |answer| %>
<%= form_tag("javascript:save_answer_comment("answer.id")", :id => 'my_answer_comment_form') do %>
<li>
<div class="commenter"></div>
<div class="add_comment">
<textarea placeholder="Enter your comment here" class="my_comment"></textarea>
</div>
</li>
<% end %>
<% end %>
I get error on this line "javascript:save_answer_comment("answer.id")"
What is the problem, please help
When placing ruby variables inside a string you need to use interpolation.
More info: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation
In your code replace
"javascript:save_answer_comment("answer.id")"
with
"javascript:save_answer_comment(#{answer.id})"

how to highlight current link in rails 3

I am trying to highlight curent link. I used this question for ideas here
I have simple menu, that is based on this structure:
<li>
<%= #waste_root.name %>
<ul>
<% #wast_child_ids.each do |it| %>
<li><%= link_to it.name, products_path(:category => it.name), class: "#{cp(products_path)} additional_class" %>
</li>
<%end%>
</ul>
</li>
In Aplication helper:
def cp(path)
"current" if current_page?(path)
end
In CSS file:
.current {
color:red;
}
What I get is all links are in red color. I don't get it. For others it worked just fine.
I think your path in view should look like this:
<li>
<%= #waste_root.name %>
<ul>
<% #wast_child_ids.each do |it| %>
<li><%= link_to it.name, products_path(:category => it.name), class: "#{cp(products_path(:category => it.name))} additional_class" %>
</li>
<%end%>
</ul>
</li>
Because, you're passing a param category in products_path and hence current_page? isn't able to judge the correct relative path. Also, it'd be better if you use the _url(complete path) instead of relative path. It'll be much clear to cross check and to understand as well.
You need to pass the category.
<%= link_to it.name, products_path(:category => it.name), class: "#{cp(products_path(:category => it.name))} additional_class" %>
They're all on the products path because you're just differentiating based on parameters. So just passing the products path they all return true, you need to differentiate based on the parameters by passing them too as you've done in your link

Converting wrapped anchor tag HTML to link_to

I'm messing around with Bootstrap. There's some HTML code which is wrapped by an anchor tag. I'm not really sure how to convert it to the Rails link_to method. I looked at the Ruby on Rails documentation for link_to but I still cannot figure it out.
Here's the code:
Dropdown <b class="caret"></b>
I tried the following but it's incorrect:
<% link_to("Users", users_path, { data: { toggle: "dropdown" }, class: "dropdown-toggle"} ) do %>
<b class="caret"></b>
<% end %>
If the href is a pure '#" but no variables at all, it's better to copy the code directly from Bootstrap than making a link_to.
If you want to add variable/method say user_path, with this complex anchor, it's better to use the block argument of link_to. Move the first argument which is usually the anchor to the block.
<%= link_to users_path, data: {toggle: "dropdown"}, class: "dropdown-toggle" do %>
<b class="caret"></b>
<% end %>

Link_to in rails and dynamic names from modal#show

I have a loop that creates a list of works from the modal work
//does work but want test to be <%= work. name %>
<ol class="meny-control mobile">
<% #works.each do |work| %>
<li class="" data-id="<%= work.id %>"><%= link_to 'test', work %></li>
<% end %>
</ol>
//doesnt work but want it to
<ol class="meny-control mobile">
<% #works.each do |work| %>
<li class="" data-id="<%= work.id %>"><%= link_to '<%= work.name %>', work %></li>
<% end %>
</ol>
As you would guess the <%= work.name %> throws a syntax error. How do I correctly format the link_to to display each work.name as the 'path' && the anchor's inner html as work.name.
Being new to rails, I'm still really iffy on understanding documentation properly. Could you please reference from link_to() (if even there) where this format is explained so I use this for future referencing & understanding --also how to properly edit the stack question title for future similar question.
The error is because of the nesting of <% tags and I suppose you already are aware of that. To solve your problem use the following:
<%= link_to "#{work.name}", work %>
The #{} is used to interpolate variables, i.e. replacing variables with their values within the string literals as in link_to "#{work.name}" above where work.name will be replaced by the value work.name holds.
you don't need "#{}".
you can write this:<%= link_to work.name, work %>