i'm trying to use a conditional inside a iteration but did not worked so, here the scenario:
in this case if the if the order or the product is present should just show the order and products with the feedback.
but even if is present show the feedback with odata and pdata.
someone know why?
<% #feedbacks.each do |feedback| %>
<% if order.present? && product.present? %>
<% order = feedback.order %>
<% product = order.product %>
<% else %>
<% odata = feedback.odata %>
<% pdata = odata.pdata %>
<% end %>
I guess this is what you are trying to do,
<% #feedbacks.each do |feedback| %>
<% if (order = feedback.order).present? && (product = feedback.product).present? %>
<%= order.title %>
<%= product.title %>
<% else %>
<%= (odata = feedback.odata).name %>
<%= odata.pdata.name %>
<% end %>
<% end %>
Note: title and name are assumed columns, replace it with your required/respected attribute.
Please go through this to understand the difference between various erb tags.
Comparison:
for the if condition you are trying to call order and product directly, which will throw error as they are related to feedback.
<% %> just executes the ruby code, you wanted to print the data so need to use <%= %>.
no need to save them in variable when you are not going to use it. I have saved them while checking the existence of the object in the condition and could use to display without querying the db.
Related
#complete[] and #incomplete[] are two array then I need to used with these array with checkbox complete array is by default true and incomplete by default false .
<%= form_with(model: #task, local: true) do |form| %>
<% #complete = Array.new %>
<% #incomplete = Array.new %>
<% #tasks.each do |task| %>
<% if task.complete != false then %>
<% #complete << task.name %>
<% else %>
<% #incomplete << task.name %>
<% end %>
<% end %>
<!-- complete task -->
<%= #complete %>
<!-- incomplete task -->
<%= #incomplete %><br>
<% #complete.each do |i| %><div id = "task_check">
<%= form.check_boX "#incomplete[]", incomplete.id %>
<%= form.check_box "chkbox_ary[#{i}]" , {checked:true} %>
<%= form.submit "update"%>
<%= content_tag(:strike, i)%>
<br></div>
<% end %>
<br> <hr>
<script>
<% #incomplete.each do |i| %>
<%= form.check_box "chkbox_ary[#{i}]" %>
<%= form.submit "update"%>
<%= i %><br>
<% end %>
<% end %>
If I check the box as false in complete then it should append to array called complete and if I check the box true in incomplete then it should append to second array called complete.
if I may, I'd like to point out some stylistic issues in your code first:
it is unusual to compare with false, i.e., if task.complete != false should be just if task.complete this works thanks to the concept of Truthiness in Ruby
you can just leave off the then in an if-conditional, that's what most Ruby developers do, i.e., if task.complete then can just be if task.complete
view templates (.erb files) should contain as little logic as possible, i.e., extract the creation of the #incomplete and #complete arrays into your controller or some other place
Ruby Enumerable has many methods to make common work easier, for instance splitting a collection into two collections can be done with partition, i.e., you can do #complete, #incomplete = #tasks.partition(&:complete) to populate the complete and incomplete arrays (&:complete is a shorthand for { |task| task.complete }, google Ruby symbol to proc if you want to know more about it)
I'm not sure I understand your question as it is. At the time the complete and incomplete arrays exist no user interaction takes place. I.e., nobody can click anything. When the HTML is rendered, no arrays exist anymore.
So there's two ways I can interpret your question:
You wish to move the DOM elements around => you need to use JavaScript for that
You wish to partition the checkboxes when the form is submitted => you need to show us the code in your controller that handles the form-submit for us to help you out
Which is it?
PS: I know this isn't an answer right now, but I could not write this as a comment since it's too long and a comment would have been illegible.
In my controller I have:
#payment_types = [['Cash' ,'1'], ['Card', '2']]
What I'm trying to achieve is to show in view Cash and Card while writing on database 1 and 2.
In my view I tried:
<% payment_types.each do |payment_type| %>
<%= payment_type %>
<% end %>
which shows ['Cash' ,'1'] ['Card', '2']]
How can I show instead in my view Cash or Card?
I'm not sure if I understand your question, but if you want to show only 'Cash', and 'Card', you can do it by passing another argument (responsible for hash value, I called it _ because it's a convention for unused arguments) to your block, like this:
<% payment_types.each do |payment_type, _| %>
<%= payment_type %>
<% end %>
You could also do it like this
<% payment_types.each do |payment_type| %>
<%= payment_type.first %>
<% end %>
I'm using a conditional to check if two values exists. I want to override the first conditional with the second one. Most likely if the second one exists then the first will, if that makes sense. Not the issue i'm having is using two different call methods.
What I have so far:
<% #data.each do |i| %>
<% if i.stock.present? %>
In Stock
<% elsif i.sold.present? %>
Sold
<% else %>
n/a
<% end %>
<% end %>
So, if in stock display 'In Stock', if sold display 'Sold'.
I think I have a solution, but feel free to let me know if there is a better way. This is using the unless statement and seems to display what I need.
<% #data.each do |i| %>
<% unless i.stock.present? %>
In Stock
<% else if i.sold.present? %>
Sold
<% else %>
n/a
<% end %>
<% end %>
<% end %>
I'm new to rails and I'm trying to build a view that will list the parents and related children
Ex:
Passport has many Visas
I want to list information about the passport and the visas that the passport has.
So I have
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<% end %>
<% end %>
I'm getting the error
undefined method `visa_type' for #Passport:0x000000091b8b28
It looks like rails is trying to find the property visa_type for passport, instead of in visa. How does the scope work within each? Can I force it to access visa_type from visa?
I think you're looking for the fields_for form helper. This will allow you to create fields for the relevant visa attributes. Replace your code sample with the following, and you should be all set.
<% #passport_list.each do |passport| %>
# passportFields
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
You can also iterate over the list as follows:
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
<% end %>
For more information on fields_for, check out the link I added above, and to customize further for your use case, check out the "One-to-many" section.
IMO you should always handle the null case of an object.
Something like this if you use rails (present? is a Rails function)...
<% if #passport_list.present? %>
<% #passport_list.each do |passport| %>
passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<%end%>
<%end%>
<% else %>
<p>Nothing to see here</p>
<% end %>
However if your #passport_list is backed by an ActiveRecord Query, you can handle this in the model/helper/controller by returning the .none query on the model. Note that this differs from an empty array because it is an ActiveRecord Scope, so you can chain AR queries onto it
# scope on AR model
def self.awesomeville
where(country_of_origin: "awesomeville")
end
# method queried in controller
#passport_list = Passport.all
if #passport_list.present?
#passport_list
else
Passport.none
end
# additional filtering in view is now possible without fear of NoMethodError
#passport_list.awesomeville
Whereas a ruby Array would raise an error as it would respond to the Array methods.
I'm having trouble trying to figure out when I reached the end of my query. So what I want to do is list all the records in my database that begin with the letter A which I got however I want to output a message if the query turns out blank. When I try I get a bunch of my custom messages even the query didn't turn out blank. Is there any way to tell if I've reached EOF in ruby on rails?
Sample
<div id = "content-A">
<p>A</p>
<% #animes.each do |anime| %>
<% if anime.aname.starts_with?('A') %>
<%= link_to anime.aname, {:action => 'list'} %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<%end %>
</div>
I believe you want sth like:
<% animes_group = #animes.group_by {|anime| anime.aname.to_s[0].upcase}
('A'..'Z').each do |letter| %>
<div id="content-<%= letter %>">
<p><%= letter %></p>
<% if animes = animes_group[letter] %>
<% animes.each do |anime| %>
<%= link_to anime.aname, {:action => 'list'} %>
<% end %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<% end %>
You should consider moving some of the logic to the controller here, however what is to be moved depends on many factors like whether #animes are being used anywhere else etc.