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 %>
Related
I'm using form_with to generate a html form using Ruby on Rails. In this form_with, I'm using a fields_for to generate another section of forms that are attributes of the first form.
To make things simple, though, I simply want to know how I can restrict time selections to be in 30 minute increments.
For example, I am doing:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_field :time_start %>
<%= end %>
How can I make it so I get a "step" attribute in the html time input node when I'm done? Right now, I'm trying to do:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_field(:time_start, :step=>600) %>
<%= end %>
But it's not giving me the desired result.
I found out what I needed to do. Instead of using a time_field input, you can use the time_select input, and specify steps through the minute_step keyword.
Instead of:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_field :time_start %>
<%= end %>
You can do:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_select :time_start {:minute_step => 30} %>
<%= end %>
And this solved my problem.
I'm currently trying to render a newsfeed, similar to that of FB on a Rails application I'm working on. Unfortunately, I'm not the greatest when it comes to CSS and I'm having some issues trying to display different posts. This issue occurs whether I'm using BootStrap or plain CSS. I do believe it's something to do with the loop that is created by <% #posts.each do |post| %> Currently, whenever a new post is made, it wraps inside the previous post; thus the more posts that are made, the thicker the border gets.
Image:
<% if #posts.any? %>
<% #posts.each do |post| %>
<div class="well">
<%= post.user.first_name %> <%= post.user.last_name %><br>
<% if !post.image.exists? %>
<h2> <%= post.text %> </h2>
<% else %>
<h2> <%= link_to post.text, post_path(post) %> </h2>
<%= link_to post_path(post) do %>
<p><%= image_tag post.image.url(:medium) %></p>
<% end %>
<% end %>
<% if #user %>
<% if current_user.voted_up_on?(post) %>
<%= link_to "Like", dislike_post_path(post), method: :put %>
<% else %>
<%= link_to "Like", like_post_path(post), method: :put %>
<% end %>
<% end %>
<%= "Likes: #{post.get_upvotes.size}" %>
<% if post.user == current_user %>
<%= link_to "Edit", edit_post_path(post) %>
<%= link_to "Delete", post_path(post), method: :delete %>
<% end %>
<div id='comments_div' class="comments-index">
<%= render post.comments %>
</div>
<% if current_user %>
<%= form_for [post, post.comments.new ], remote: true do |f| %>
<%= f.text_area :text, placeholder: 'Add a comment' %>
<%= f.submit 'Comment' %>
<% end %>
<% else%>
<p>You need to <%= link_to "sign in", new_user_session_path %> to comment</p>
<% end %>
<% end %>
<% else %>
No posts have been added!
<% end %>
</div>
Any help would be greatly appreciated! Thanks.
Edit: OK, please take a look at the new image -- hopefully that will make the issue slightly more obvious. Additionally, I've removed all the dead tags and replaced them with just one: BootStrap's 'well' class. So, there you have it. All the information you need is within the code above.
from your description it sounds as though an html element is not being properly closed. Run the page source through an html validator and that could show you the problem.
If you don't want to take a structured problem solving approach, try adding another </div> to the end of your posts-index container.
Your issue is very simple, just that its not clear due to poor indendation.
A simple way to explain what you did is:
<-- if (start) -->
<-- do (start) -->
<-- post (start) -->
(post is not ending here, hence it breaks the layout)
<-- do (end) -->
<-- if (end) -->
<-- post (end) -->
Mistake in the above should be simple to understand so if you move your last </div>(of the well class) just before the second last <% end %>(of the <% #posts.each do |post| %> loop) it should fix the issue. So the last few lines should be
<% else%>
<p>You need to <%= link_to "sign in", new_user_session_path %> to comment</p>
<% end %>
</div>
<% end %>
<% else %>
No posts have been added!
<% end %>
Sounds to me like it could be a misplaced
<% end %>
or a missing
</div>
that is causing this behavior.
Proper indentation will point to where to close off actions or divs
Ok, so here is my code.
<%= form_for #quiz do |f|%>
<% #questions.shuffle.each do |q| %>
# get_answers returns an array of 4 random answers
<% answers = q.get_answers %>
<%= f.label q.inquest %><br>
<% answers.each do |a| %>
<%= f.radio_button <need help here!>, a %>
<%= f.label <need help here!>, a %><br>
<%= f.hidden_field <need help here!>, q.id %>
<% end %>
<% end %>
<% end %>
my quiz table has the attributes
concept_id
user_id
how do I add custom attributes that I can have access to in the post action? It should be dynamic so it can scale with how many questions there are. I want something like this for my params.
{
user_answers: {
question1: {
:question_id
:answer
{
}
}
Depending on how many questions there are, it'll go like question1, 2, 3, 4 etc..
The hidden_field would carry with question_id while the radio_button would carry the answer. I don't need it to save or anything, I just need access to them
Ok, so I realized that I shouldn't have called those form tags on f. instead have them as their own form tags
for example:
radio_button_tag instead of f.radio_button
In my controller i find a uniq notebook and user name.
but i want to be able to check in my html code that it shows only one type of user.
in controller
def index
#allnotebooks = Note.uniq.pluck(:string, :notebook)
#notes = Note.all
end
in my html
<% #allnotebooks.each do |notebook| %>
<% if notebook.string == c_user.name %>
<option><%= notebook %></option>
<% end %>
<% end %>
notebook.string does not work. what am i missing
Also you can do in different way other than using pluck, using select you can do it
like-
In controller code-
def index
#allnotebooks = Note.uniq.select([:string, :notebook])
#notes = Note.all
end
in your html
<% #allnotebooks.each do |notebook| %>
<% if notebook.string == c_user.name %>
<option><%= notebook %></option>
<% end %>
<% end %>
Thanks!!!
Pluck returns an array.
Try notebook.first or notebook[0]
Docs here: http://apidock.com/rails/ActiveRecord/Calculations/pluck
Second example at the bottom applies here.
In your example it should be:
<% #allnotebooks.each do |notebook| %>
<% if notebook[0] == c_user.name %>
<option><%= notebook[1] %></option>
<% end %>
<% end %>
btw you might want to improve this by only loading notebooks where the string equals your user's name.
I am not a good ruby guy, You can write it like
<% #allnotebooks.each_with_index do |notebook, index| %>
<%= notebook[index].string %> => <%= notebook[index].notebook %>
<% end %>
for "a" unique notebook, you do you need to loop it?
just to say:
#allnotebooks.each{|notebook| <%= notebook[0] %> <%= notebook[1] %> }
But there can be better ways.
I've been running my head into a wall for hours now and I can't make this work. I'm trying to add a bunch of check boxes to verify items on my index page. I found this old rails cast that does exactly what I want to do, but I've run into a problem. Anything enclosed inside my form is removed from the index page, like just gone poof. Here's the code from the index view.
<% form_tag verify_products_path, :method => :put do %>
<% #products_unverified.each do |products| %>
<% if product.deleted != 'true' %>
<tr data-link="<%= product_path(product) %>">
<td><%= product.name %></td>
<td><%= product.description %></td>
</tr>
<% end %>
<% end %>
<%= submit_tag "Mark as Verified" %>
<% end %>
</tbody>
Here's the routes stuff
resources :products do
put :verify, :on => :collection
end
and the controller just has a dummy method for now.
def verify
end
Any clue as to why the index view blanks out when the form is introduced? Any help is much appreciated.
You forgot the = before form_tag:
<%= form_tag verify_products_path, :method => :put do %>
put an = in front of form_tag
instead of
<% form_tag verify_products_path, :method => :put do %>
try
<%= form_tag verify_products_path, :method => :put do %>