Shows "#" instead of text - ruby-on-rails

I am executing this code:
def find_all_from_id
Note.find_by_sql([%{SELECT NOTE_N FROM NOTES WHERE ORDSP_ID = #{#order.order_number}}])
end
And in ERB file, I have an output:
<div class="form-field notes-div" >
<%= service_form.label :notes, "Pakalpojuma papildinformācija:", :class => "label_for_cod", :style=>"width: 170px;" %>
<div style="float:left; width:400px;"><%#= notes %> <%= find_all_from_id %></div>
</div>
It puts the # symbol instead of text the that comes from the database (http://prntscr.com/kop3mz). Why? And how can I fix that?

Take a look at the documentation for find_by_sql: https://api.rubyonrails.org/classes/ActiveRecord/Querying.html
This method will return an array of instances. If you want to display information about those instances you need to iterate through the array and manually display the information you want to show. Erb will not display the array of instances, leading to your issue.
E.g.
<% find_all_from_id.each do |note| %>
<%= note.name %>
<%= note.id %>
<% end %>

Related

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

Rails Helper create unintended Array ouput

I have created a rails helper method, So far so good the only problem is it created an unexpected array ouput.
I did try the key value pair using the each method but the array still there.
I'm trying to figure it out how to remove the unexpected array
My application_helper.rb
def bid_items(origin, destination)
item = Item.where(item_deliver_from: origin).where(item_deliver_to: destination).where(shopper_id: current_user)
end
My search_results.html.rb
<%= bid_items(trip.origin, trip.destination).each do |item| %>
<div class="card border-0">
<%= image_tag item.cover_image_url(:cover_image_medium), class: "card-img-top" %>
<div class="card-body">
<h5 class="card-title"><%= item.name %></h5>
</div>
</div>
<% end %>
Though I got the intended results still, there's an unexpected array
check the image
I just want to remove this area
See Red X
Remove = sign from <%= bid_items(trip.origin, trip.destination).each do |item| %>
So, make it <% bid_items(trip.origin, trip.destination).each do |item| %>

Ruby on Rails: How to print contents of variable in view and make checkbox?

I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>

Check_box_tag not displaying labels properly

Everything is posting correctly, but I do not see any labels in my checkboxes, just blanks. My form looks like this:
<%= form_for #itemrecord do |f| %>
<div class="col-xs-12">
<p><b>Items people are asking for</b></p>
</div>
<% #wishlist.each do |category, list| %>
<div class="col-xs-2">
<div class="form-group box">
<h5> <%="#{category}"%> </h5>
<% list.each do |thing| %>
<%= check_box_tag ":item_name[]", "#{thing}" %>
</br>
<% end %>
</div>
</div>
<% end %>
<%= f.submit "Go!", class: "btn btn-primary btn-large btn-block" %>
</div>
<% end %>
What's happening is that wishlist is a hash of categories and items within those categories that I set in the controller, and is then called by multiple form builders to build checkboxes. The challenge is that right now in the current implementation, the checkboxes params are passed through properly (FYI controller code is at the bottom), but beside each checkbox, there is no text that shows the thing (i.e., no label so that people know what they're checking.
Here's the html generated for one checkbox (it's the same for all checkboxes)
Basically, I need to make the value the label.
FYI what's happening is that for every item checked, a record is being created. Here's the controller code:
def create
items_to_be_saved = []
inventory_params.each do |i|
items_to_be_saved << ({ :signup_id => Signup.find_by_email(session[:signup_email]).id, :item_name => i })
end
if Inventory.create items_to_be_saved
flash[:success] = "Thanks!"
redirect_to root_path
else
render new_inventory_path
end
end
def inventory_params
params.require(":item_name")
end
In your code:
<%= check_box_tag ":item_name[]", "#{thing}" %>
Second parameter for check_box_tag is not a label value, it is just a value which goes to controller in parameters. If you wan't to display label within your checkbox you will need to call label_tag in your view:
= label_tag ':item_name[]', thing
= check_box_tag ':item_name[]'
But you definitely should check simple_form gem which allows you to render checkboxes in much cleaner way:
f.input :field, as: :boolean, inline_label: 'Label'
can you please try it and let me know
<%= check_box_tag 'item_name[]', thing %>

How do I run an array through an IF statement in rails?

I am creating an application that highlights user messages from a stream based on whether or not the user has been 'vouched'. It works fine if it's setup for a single author. For example
controller: #vouch = Vouch.last.vouched_user_nickname
view:
<% Twitter::Search.new(params[:id]).each do |tweet| %>
<li>
<%= image_tag tweet.profile_image_url %>
<% if #vouch.include? tweet.from_user %> <div class="flit_message_containerh">
<u> <%= tweet.from_user %></u> <%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% else %> <div class="flit_message_container">
<u> <%= tweet.from_user %></u>
<%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% end %>
But I'm having trouble doing it for multiple user nicknames.
#vouch = Vouch.find(:all,
:select => "vouched_user_nickname",
:group => 'vouched_user_nickname'
)
Any ideas would be greatly appreciated. I'm a rails noob.
Assuming there isn't a relation between your Vouch model and the Twitter source (I haven't used that gem/plugin yet so I don't know), one solution is to pull all the Twitter entries you want and all the vouches in the controller and do the check in the view.
controller:
#tweets = Twitter::Search.new(params[:id])
#vouches = Vouch.find(:all)
view:
<% #tweets.each do |tweet| %>
<div class="flit_message_container<%=
#vouches.any? { |v| v.vouched_user_nickname == tweet.from_user } ? "h" : ""
%>">
...
</div>
<% end %>
#vouch = Vouch.find_by_vouched_user_nickname(:all, ["nickname1","nickname2"])
Your problem seems to be that you are not looping through the array, so how can it decide if certain elements meet the criteria you set?
Example, in your view:
<% for vouch in #vouch do %>
<% if vouch.include? tweet.from_user %>
<div class="flit_message_containerh">
<u> <%= tweet.from_user %></u> <%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% else %> <div class="flit_message_container">
<u> <%= tweet.from_user %></u>
<%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% end %>
<% end %>
I see several problems.
The first one is this:
#vouch = Vouch.last.vouched_user_nickname
You are using a variable called #vouch to store a user nickname. That is counterintuitive and will confuse other people reading your code (like myself). Use something like this instead:
#vouch = Vouch.last #on the controller
#vouch.vouched_used_nickname #on the view
This ... eum ... "exotic" naming convention helps confusing yourself when you try to do the "multiple" example:
#vouch = Vouch.find(:all,
:select => "vouched_user_nickname",
:group => 'vouched_user_nickname')
Activerecord's find(:all, ...) will allways return an array of activerecord objects (or an empty array). You seem to be expecting an array of strings. You will allways get Vouches if you do Vouch.find.
The :select part just limits the amount of information these vouches have (they only come with vouched_user_nickname populated. The rest, including their id, is empty, because it is not read from the database).
If you want to have an array of user nicknames you can do it like this:
# note the names. #vouchers in plural, and #nicknames for the user names
#vouchers = Vouch.find(:all, :select => "vouched_user_nickname",
:group => 'vouched_user_nickname')
#nicknames = #vouchers.collect{|v| v.vouched_user_nickname}
Is your problem that you don't know the correct controller code to write to find the #vouch array? Or is it that you don't know what to do with the array once you get it?
view: <% if #vouch.include? tweet.from_user %>
.include? is a method you can call on either a single object or an array of objects if tweet.from_user has an object that is also included in the #vouch array to get the #vouch array in your controller you should:

Resources