Link_to in rails and dynamic names from modal#show - html

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 %>

Related

How to put array in an I18n in html.erb

I am working with a t('footer.card.type.name') where I want to replace name with the array list. t('footer.card.type.name') by the way, is to show different languages in different I18n.
Currently I am stuck with placing |type| into t('footer.card_type.%{type}'). This I am sure it does not work. I am just experimenting on ways to making it work.
<%= link_to t('footer.card_type.all'), credit_cards_path %>
<% ['cash_back', 'islamic', 'petrol', 'reward', 'travel', 'no_annual_fee', 'premium', 'balance_transfer', 'promo'].each do |type| %>
<%= link_to t('footer.card_type.%{type}'), credit_card_type_path(sub_type: type.gsub('_','-')) %>
<% end %>
I have found a work around by adding .concat(type) after the t('footer.card_type.'). It successfully returns the translation of both languages. Hope this code can help someone.
<% ['cash_back', 'islamic', 'petrol', 'reward', 'travel', 'no_annual_fee', 'premium', 'balance_transfer', 'promo'].each do |type| %>
<%= link_to t('footer.card_type.'.concat(type)), credit_card_type_path(sub_type: type.gsub('_','-')) %>
<% end %>

How to link_to an html anchor tag

I have been trying to make a number of lists where after clicking each list its content gets edited. I'am using twitter bootstrap, embedded HTML in this Ruby on Rails app.
<div class="list-group">
<% #statuses.each do |status| %>
<%= status.content %>
<% end %>
</div>
Here i did not get how to get these <%= link_to to get connected with each <a href="" URL's of the status.
<%= link_to 'Edit', edit_status_path(status) %>
Please help i m totally confused.
Thanks in advance.
If you want the entire status to actually be a link, like you did with a manual anchor tag in your example, then try:
<div class="list-group">
<% #statuses.each do |status| %>
<%= link_to status.content, edit_status_path(status), class: "list-group-item" %>
<% end %>
</div>
Also see http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to.

undefined method `stringify_keys' (ROR)

How to correct write this record? When I write the following code, which you can see in this post I have received following mistake: undefined method `stringify_keys' for "/plant/index":String
Html code
<li class="new_button">
<%= link_to "Plants", plant_path, :class=>"greens" do %>
<span></span>
<% end %>
</li>
I need (just with link_to)
<li class="new_button">Plants<span></span></li>
If you pass block into link_to, first argument is responsible for evaluating proper link href and second argument is assumed options. In your example, second argument is returned value of plant_path, which is String instance, but Rails want to evaluate it as Hash. What you should do is:
<%= link_to plant_path, class: 'greens' do %>
Plants<span></span>
<% end %>
Try this
<%= link_to plant_path, :class=>"greens" do %>
<span><%= "Plants" %></span>
<% end %>

Rendering view for an element in rails by :id

I have a sidebar that contains links to all of a users :shopping_lists. Upon clicking on one of those links, I'd like to render a page showing the :list_items in that particular list. Here's my sidebar partial:
<aside class="sidebar-nav-fixed">
<h1>My Lists</h1>
<% if user_signed_in? %>
<% current_user.shopping_lists.each do |l| %>
<ul>
<%= link_to "#{l.name}", '#' %>
</ul>
<% end %>
<% else %>
<h5><%= link_to "Sign in to manage lists.", new_user_session_path %></h5>
<% end %>
</aside>
My question is: what path would I be putting in place of my current stub link in order to route to the correct list? Thanks in advance!
That will depend on how your routes are setup. I would expect shopping lists to always be in the context of a user, so probably something like this:
<%= link_to l.name, user_shopping_list_path(current_user, l) %>
If shopping lists are a top level route, then probably something like this:
<%= link_to l.name, shopping_list_path(l) %>
There are couple of things you can do, granted your routes are setup correctly:
The easiest is:
link_to "#{l.name}", l
Rails should create a link something similar to http://host/shopping_lists/2
The above is a shorthand for
link_to "#{l.name}", shopping_list_path(l)
To see a list of available routes and methods you can run:
bundle exec rake routes
in the root of your rails app

Writing HTML code with Rails

<% #ticket.conversations.each do |c| %>
<section class="messages">
<%="<li> #{c.the_message} </li>" %>
</section>
<%end%>
I am trying to have rails write the HTML code for me so the output would look something like this:
<li>MESSAGE1</li>
<li>MESSAGE2</li>
<li>Next message here...</li>
I am going to style every nth element to have a different style to show what speaker it belongs to. But currently is just outputs straight text and escapes the HTML. How do I stop this escape?
To output you need to use <%= as follows within your <section> block:
<%= "<li> #{c.the_message} </li>".html_safe %>
But currently is just outputs straight text and escapes the HTML
You can use the html_safe method. Please refer to the "Extensions to String" topic in this document: http://guides.rubyonrails.org/active_support_core_extensions.html
Another option you can use is the raw helper(as pointed out by Stefan) which calls the html_safe for you. e.g.
<%= raw "<li> #{c.the_message} </li>" %>
You can also style your list items this way:
<li><%= c.the_message %></li>
Just based upon preference.
Try it this way:
<% #ticket.conversations.each do |c| %>
<section class="messages">
<li><%= c.the_message %></li>
</section>
<% end %>
Or if you don't want to repeat <section> every time:
<section class="messages">
<% #ticket.conversations.each do |c| %>
<li><%= c.the_message %></li>
<% end %>
</section>