How to display current username as a Link in RUBY? - ruby

<%= link_to ' ', user_path(current_user) %>
How do I make it so <% current user %> displays the current user as a link.
Because in ruby whatever goes inside Link to is written as a string and not displayed as Ruby's answer.

Use below code replace name with field that stores user's name
<%= link_to current_user.name, user_path(current_user) %>

Related

ruby form data receive

Guy
this is just simple form in ruby
<%= form_tag("/page/create", method: "get") do %>
<% 5.times do %>
<%= text_field_tag :Name,(params[:Name]), size: 10 %><br>
<% end %>
<%= submit_tag("send") %>
<% end%>
why in my control the #Name=params['Name'] not work with me well ?
always not gives me any thing ?
You are posting the same paramter multiple times. This leads to an empty params['Name'], except you fill out the last of the input fields.
Most frameworks discard multiple parameters with the same name, using only the last one in the parameter list.
If you want to pass an array of fields to your controller, name your field with trailing open-close square brackets like this: text_field_tag 'Name[]'. This makes Rails populate params['Name'] with an array of your input's values.
Note that :Name[] is not a valid Symbol in Ruby, so you'll have to use a String as the first argument to the text_field_tag helper.
I think you have a few things going on here:
Changing <%= text_field_tag :Name,(params[:Name]), size: 10 %><br> to <%= text_field_tag :Name, size: 10 %><br> might be the first step in moving forward.

Rails- Index in form_for field names

I have a form that I am trying to build to edit multiple records. It's complicated, doesn't map straight to the database, and there can be any number of records. I have the code written so all of the data is passed to the view as a hash. Like this:
#formdata = {"datafield_1"=>"value_1", "datafield_2"=>"value_2"}
What I want to do is to create something like:
f.textfield :datafield_1
f.textfield :datafield_2
f.textfield :datafield_3
etc. etc. etc.
But I don't know how to pass the index of my for loop into the variable name. In short, how do I do :datafield_i where i is my index?
<% %w(1 2 3).each do |i| %>
<%= f.textfield(:"datafield_#{i}") -%>
<% end %>
or
<% #formdata.keys.each do |datafield| %>
<%= f.textfield(datafield.to_sym) -%>
<% end %>

Ruby if else syntax

I have an app where users can sign up with Facebook, in which case I take the Facebook image, or they can sign up using regular authentication, in which case they're uploading a photo using CarrierWave gem.
Because of some sort of conflict, I had to create a column in the database (:image)to store the url to the Facebook image, and then a different column (:dimage) to store the url for the image if they signed up using the non-facebook sign up.
So when it comes time to display the user, I'm checking to see if there's an :image, and, if not, then displaying the other :dimage.
However, I don't want to require them to upload an image, in which case, I want to display an anon image (here represented by the rails.png). However, the Rails.png isn't showing up (just a broken image path), so I'm assuming there's some sort of error with my if else syntax because the image_tag("rails.png") is taken straight from the api
<% if user.image %>
<%= image_tag user.image %>
<% elsif user.dimage %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
The generated html on the rails.png
<img alt="Assets" src="/assets/">
Use the present? method to check if the attribute is not empty:
<% if user.image.present? %>
<%= image_tag user.image %>
<% elsif user.dimage.present? %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
You'll save yourself a headache or two because in Ruby the only falsehoods are false and nil. This means that the empty string "" will actually return true.
You need to look at look into user.image.nil? user.image.empty? and user.image.blank? depending on what you're using and apply the same to the rest of your conditional. You need to find out specifically what your looking for or the if else won't give you what you want. I would try empty first which checks if something is nil or is an empty string.
When you create a new rails application, rails stores its logo in PROJECT_PATH/app/assets/images folder. Could you check if that image exists?

Iterate Form fields

I have build a module to add translations for each standard topic. Theses topic got many standard options and you can translate it directly in page.
I got an issue with my form about the edit view.
When i display a translation it's repeat all value of the f.input :value each time he have one and i want it to display with the each of standard value.
The question is how i can iterate my input field :value in the form to display only once per standard value and not repeat all value translated by standard value.
when i want create a new one all workings fine. It's just about the iterate field who is repeated how many times he got a field in the table.
the gist for my code :
https://gist.github.com/266562670cd8dab28548
Change:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first, option do |translate_form| %>
to:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first || option.preference_topic_option_translations.build, option do |translate_form| %>

Ruby On Rails XML to View

I have a controller with an API request showing all my Google Docs.
feed = client.get('http://docs.google.com/feeds/documents/private/full').to_xml
feed.elements.each('entry') do |entry|
puts 'title: ' + entry.elements['title'].text
puts 'type: ' + entry.elements['category'].attribute('label').value
puts 'updated: ' + entry.elements['updated'].text
puts 'id: ' + entry.elements['id'].text
# Extract the href value from each <atom:link>
links = {}
entry.elements.each('link') do |link|
links[link.attribute('rel').value] = link.attribute('href').value
end
puts links.to_s
end
So, I can see the results in my console but how do I get them into my view?
I tried with something like this, but that doesn't work (I changed my variable in the controller to an accessor of course)
<% feed.elements.each('entry') do
|entry| %> <%
entry.elements['title'].text %> <%
end %>
First, in your controller, make feed an instance variable. IE: it should be:
#feed = client.get..... instead of feed = client.get....
If that doesn't fix it... I don't know your API for sure, but I suspect you may need to be using:
<% #feed.elements.each('entry') do |entry| %> <% entry['title'] %> <% end %>
Note: entry['title'] instead of entry.elements['title'].text
What your current code indicates is that the feed is structured like this:
feed.elements[0].elements['attr'].text, when it's probably just feed.elements[0]['attr']
Does that make sense? Try that and see what happens.
If that doesn't work, just put: debug(#feed) in your view and copy and paste it to the end of your question. That'll help us figure out the right way to access this info.
Problem solved. Because I use 'puts' in the controller to show the content of the feed in the console I also have to change that for the view. Of course, puts is equal to <%= ... %>.
<ul>
<% #feed.elements.each('entry') do |entry| %>
<li><%= 'title: ' + entry.elements['title'].text %></li>
<% end %>
</ul>

Resources