I'm sure this has already been asked and it's pretty easy but I don't really know what to search to find a solution.
If I have values in a column table like 1,-1,2 where -1 = sale, 1 = purchase etc, how do I show purchase instead of 1 or sale instead of -1?
Database table stockmovements column name reason.
Whenever I face such situation I create a readable_method in model.
In your model class add method like this:
def readable_reason
case reason
when -1
"sale"
when 1
"purchase"
end
end
Then In your view,
<td>stockmovement.readable_reason</td>
It keeps my view file more readable. Also I don't have to copy case-when in every view where I want to show reason.
You can do like this:
<% if stockmovement == 1 %>
purchase
<% elsif stockmovement == -1 %>
sale
<% end %>
Inside your table's document you should put:
<td>
<% case stockmovement %>
<% when 1 %>
"purchase"
<% when 2 %>
"sale"
<% end %>
</td>
Related
In my application a user can have a group with many alerts. When an alert is created it has an t.boolean "read", default: false attribute, which I want to set to true once the users has seen it.
I have the following:
groups_controller.rb
class GroupsController < ApplicationController
def index
#groups = current_user.groups.includes(:alerts, :new_alerts)
end
end
index.html.erb
<% #groups.each do |group| %>
<% group.alerts.limit(5).each do |alert| %>
<% if alert.unread? %>
<span class="badge pending-status">New</span>
<% end %>
...
<% end %>
<% end %>
And in the show.html.erb on a group I show all the alerts.
I want to know if it's possible to mark those 5 in index.html.erb as read once they have been loaded into the view? So if I have 5 alerts and 3 are unread, I want the 3 to be shown as New, but once the page is refresh or show.html.erb is loaded, they will be shown an read.
Any suggestions?
I have been fiddling with an idea of having the read attribute as an integer instead, with a default of 0 and first time they are loaded alert.update(read: 1) and then alerts with read > 2 would be the unread ones.
What do you think about that?
I'm having trouble figuring out how to display an index.
In my organisation requests view folder, I have a file called index.html.erb.
In that file, I'm trying to list each organisation request. I've tried each of the following formulations:
<% OrganisationRequest.each do |OrgReq| %>
<% organisation_request.each do |OrgReq| %>
<% #organisation_request.each do |OrgReq| %>
<% #organisation_requests.each do |OrgReq| %>
In each case, I get an error that says:
formal argument cannot be a constant
I thought a constant meant something beginning with a capital letter. 3 of the above attempts don't begin with a capital letter.
It's also confusing to me since in my user index, I have <% User.each %> and I don't get an error message.
Can anyone see what's gone wrong? How do I ask for a list of objects?
If you have your data and view right, you should be able to fix with:
<% #organisation_requests.each do |org_req| %>
...
<% end %>
If we stick Rails conventions, we'd say that, you have a OrganisationRequests controller, has such content.
class OrganisationRequestsController < ApplicationController
...
def index
#your_local_variable = OrganisationRequest.find(...)
end
...
end
That is to say, you need to use, #your_local_variable inside view file.
<% #your_local_variable.each do |o| %>
....
<% end %>
If the variable inside index action is #organisation_requests, use that.
I have an isolated issue.
I have a table that populates from several different models, it creates links to follow to each respective view.
The code that I have made for each link should be the same, but for some reason, the link isn't showing up under 'Baseline'. I've checked the :create methods for each model, and they mimic each other, and the code from the view is also just a copy - so I'm at a loss as to where to look next. I'm sure that the problem is that the create method is failing, but I don't know where/how.
Here is the code from my view (I'm also pasting the code from FollowUp3Week, because it works):
<% if Baseline.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", baseline_path([Baseline.where(subject_id: sub.subject_id).first]) %>
<% else %>
<%= Baseline.create(subject_id: sub.subject_id) %> #I left the equal for the screenshot.
<% end %>
</td>
<td>
<% if FollowUp3Week.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", follow_up3_week_path([FollowUp3Week.where(subject_id: sub.subject_id).first]) %>
<% else %>
<% FollowUp3Week.create(subject_id: sub.subject_id) %>
<% end %>
</td>
And here is the create method from baselines_controller.rb
def create
#baseline = Baseline.new(params[:baseline])
if #baseline.save
flash[:success] = "Baseline added from create method"
redirect_to baselines_url
else
render 'new'
end
end
I'm also attaching an image of what it looks like. If I remove the equal sign from <%=, the cell will be blank.
EDIT. I'm in the process of removing all of my database queries from the view. Thank you for your comments.
You should really get that Baseline.where out of your view and into the model. AR scopes from the view is a serious no-no in Rails.
In your baseline mode you could do something like:
def empty_subject(subject_id)
where(subject_id: subject_id).first != nil
end
Also, it looks like you're passing arrays into baseline_path and follow_up3_week_path.
Ditch the square brackets.
on Baseline model, put this
def display_name
"#{name}" #whatever you like to show including link
end
I am building a simple app and in many views I am displaying all of the objects associated with a certain model (many-to-one relationship). For example, I have a house model and an Item model where House has many Items. On the Show view for house I have the following code:
<% #house.items.each do |item| %>
<% if item.needed == true%>
<p>
<%= item.description %>
</p>
<% end %>
<% end %>
and this displays all the items along with one blank item. If I delete all the items, leaving an empty array there is still one empty item remaining. I can hack this using the code:
<% #house.items[0..-2].each do |item| %>
<% if item.needed == true%>
<p>
<%= item.description %>
</p>
<% end %>
<% end %>
This is probably a really simple question, but I would like to avoid using the latter code, and would like to understand why this is happening. Thanks.
The issue you are seeing is data related. This is to say, you need to figure out what is being returned by #house.items. Perhaps you have an item that has needed == true and a blank description? To trouble shoot this verify what is being returned by the house object in question by opening up the rails console, loading the house object in question and checking what is returned by house.items.
When using #house.items.new to set up a new Item object, it will alter the #house.items array, even though the new item is not yet persisted to the database. Example:
items = #house.items
items.length
# => 3
item = #house.items.new
items.length
# => 4
You could either add a check inside your loop for something like if item.persisted? or unless item.new_record?. Or, you could build the new item this way instead, which won't include it in #house.items until it's actually saved to the database:
item = Item.new(house_id: #house.id, needed: true)
Is it possible to call the include? function on a whole table, like this?
<% #user.games.each do |g|
##latestround = g.rounds.order('created_at DESC').first
%>
<% if ##latestround.submittedpictures.isFinalPicture.include?(true) %>
<p>FinalPicture has been played!</p>
<% end %>
<% end %>
The problem i'm getting is that It only works when I put a block on submittedpictures and then loop through each record of this table. However I want to look through the whole table in one go and see if the column 'isFinalPicture' includes a value with 'false'.
Any ideas?
The following snippet works but its not the way i want it (I would get more lines if the round happens to have more 'true' FinalPictures)
<% ##latestround.submittedpictures.each do |s| %>
<% if s.isFinalPicture == true %>
<p>Final Picture has been played!</p>
<% end %>
<% end %>
You could make a scope for it like
class SubmitedPricture << ActiveRecord::Base
scope :final_pictures, where('isFinalPricture = ?', true)
end
then you could see if there is any with only one query
latestround.submittedpictures.final_pictures.any?
Also you should follow the conventions of Rails in naming your Models and everything else. Like submittedpictures should be submitted_pictures