Rendering variable in Ruby on Rails not correct - ruby-on-rails-3

I am trying to render something from my controller action.
def show
#bookmark = Bookmark.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #bookmark }
end
end
This above is the action in the controller.
The html erb file for show is :
<p>
<b>Url:</b>
<%= #bookmark.each do |book| %>
<li><%= book.url%>
<b>Title:</b>
<li><%= book.title%>
<% end %>
</p>
I am getting output too, but additional to that I am getting extra things. The output is :
Url:
www.google.com Title:
h
www.gmail.com Title:
hiw
asd Title:
as
sd Title:
ad
asd Title:
as
asd Title:
as
asfa Title:
ada
www.mrabhiram.github.com Title:
abhi
www.mrabhiram.github.com Title:
abhi
www.mrabhiram.github.com Title:
abhi
[#<Bookmark id: 1, title: "h", url: "www.google.com", tags: "php,python", created_at: "2012-12-12 06:02:21", updated_at: "2012-12-12 06:02:21">, #<Bookmark id: 2, title: "hiw", url: "www.gmail.com", tags: "mail", created_at: "2012-12-12 06:02:41", updated_at: "2012-12-12 06:02:41">, #<Bookmark id: 3, title: "as", url: "asd", tags: "asd", created_at: "2012-12-12 06:07:41", updated_at: "2012-12-12 06:07:41">, #<Bookmark id: 4, title: "ad", url: "sd", tags: "dsfsd", created_at: "2012-12-12 06:11:04", updated_at: "2012-12-12 06:11:04">, #<Bookmark id: 5, title: "as", url: "asd", tags: "asd", created_at: "2012-12-12 06:22:36", updated_at: "2012-12-12 06:22:36">, #<Bookmark id: 6, title: "as", url: "asd", tags: "asda", created_at: "2012-12-12 06:25:33", updated_at: "2012-12-12 06:25:33">, #<Bookmark id: 7, title: "ada", url: "asfa", tags: "fa", created_at: "2012-12-12 07:41:16", updated_at: "2012-12-12 07:41:16">, #<Bookmark id: 8, title: "abhi", url: "www.mrabhiram.github.com", tags: "abhiram,php", created_at: "2012-12-12 07:45:29", updated_at: "2012-12-12 07:45:29">, #<Bookmark id: 9, title: "abhi", url: "www.mrabhiram.github.com", tags: "abhiram,php", created_at: "2012-12-12 07:46:19", updated_at: "2012-12-12 07:46:19">, #<Bookmark id: 10, title: "abhi", url: "www.mrabhiram.github.com", tags: "abhiram,php", created_at: "2012-12-12 07:48:28", updated_at: "2012-12-12 07:48:28">]
I should get rid of the array printing at last. I only want the records to be printed.

Use <% ... %> instead of <%= ... %>:
<% #bookmark.each do |book| %>
See: eRB <%=
Update (in response to comments):
To avoid an error if there is only one bookmark, wrap the instance variable in Array():
<% Array(#bookmark).each do |book| %>
Incidentally I would suggest renaming your instance variable from #bookmark to #bookmarks.

Related

ejs print value of array from specific id

I send an array of object called gudangs from my controller app.js like this
res.render('daftar-staff', {
title: 'Daftar Staff',
session: req.session,
layout: 'layouts/main-layouts',
gudangs
})
Its successfully sent to the ejs view and I received
{ _id: new ObjectId("61a804c4059d1d90797c8ad0"), nama: 'gudang benowo', alamat: '', __v: 0 },{ _id: new ObjectId("61a804ecdfa6962a0162850f"), nama: 'gudang pakal', alamat: '', __v: 0 },{ _id: new ObjectId("61a86fb98b3cd7f948a388b7"), nama: 'gudang tandes', alamat: 'tandes no 19', isFull: 'true', __v: 0 },{ _id: new ObjectId("61a872c07cc01577ddf78b53"), nama: 'gudang kepatihan', alamat: '', isFull: 'true', __v: 0 },{ _id: new ObjectId("61a954eee3c45454070d4b10"), alamat: 'jalan benowo 13', __v: 0 },{ _id: new ObjectId("61a955954e757a01405e44b4"), alamat: 'jalan benowo 13', __v: 0 }
What i want is to print nama on specific gudang _id
Right now I can accomplish this by iterate through all gudangs array like this
<% gudangs.forEach((gudang,i) => { %>
<% if(gudang._id==='xxxx'){ %>
<%= gudang.nama %>
<% } %>
<% } %>
I wonder is there any simple way to do this?
Using JS array filters
<%= gudangs.filter(({ _id })=> {return _id === 'XXX'})[0]['nama'] %>
gudangs = [{ _id: "61a804c4059d1d90797c8ad0", nama: 'gudang benowo', alamat: '', __v: 0 },{ _id: "61a804ecdfa6962a0162850f", nama: 'gudang pakal', alamat: '', __v: 0 },{ _id: "61a86fb98b3cd7f948a388b7", nama: 'gudang tandes', alamat: 'tandes no 19', isFull: 'true', __v: 0 },{ _id: "61a872c07cc01577ddf78b53", nama: 'gudang kepatihan', alamat: '', isFull: 'true', __v: 0 },{ _id: "61a954eee3c45454070d4b10", alamat: 'jalan benowo 13', __v: 0 },{ _id: "61a955954e757a01405e44b4", alamat: 'jalan benowo 13', __v: 0 }];
console.log(gudangs.filter(({ _id })=> {return _id === '61a804ecdfa6962a0162850f'})[0]['nama']);
This is a working snippet. Remember that ObjectId is not defined in frontend Javascript.

How to get single value from array in VueJs

Tell a beginner web delepoer how to get all title from all objects in array
this is my array
0: {id: 6, category_id: 2, title: "Test", brand: "Test", serial_number: "2165412315864",…}
1: {id: 7, category_id: 3, title: "Test2", brand: "Test2", serial_number: "2165412315864",…}
2: {id: 8, category_id: 5, title: "New", brand: "New", serial_number: "2165412315864",…}
3: {id: 9, category_id: 1, title: "New2", brand: "New2", serial_number: "2165412315864",…}
Im try to use this code
categories: {
handler(categories) {
console.log('categories: ', categories[title]); //Debug
},
deep: true
}
If you want an array with just the titles you can use Array.map and pick the parts your interested in:
const arr = [{
id: 6,
category_id: 2,
title: "Test",
brand: "Test",
serial_number: "2165412315864"
},
{
id: 7,
category_id: 3,
title: "Test2",
brand: "Test2",
serial_number: "2165412315864"
},
{
id: 8,
category_id: 5,
title: "New",
brand: "New",
serial_number: "2165412315864"
},
{
id: 9,
category_id: 1,
title: "New2",
brand: "New2",
serial_number: "2165412315864"
}
];
const titles = arr.map(({ title }) => title);
console.log(titles);
The categories variable is actually an array here, so you can not use
categories['title']
// Or,
categories.title
here. To get all title property for each category inside the categories array you can use array .map() method like:
categories: {
handler(categories) {
const arr = categories.map(c => c.title)
console.log('titles: ', arr); //Debug
},
deep: true
}

Vue Tables 2 custom row style

So I'm using vue tables 2 and found little problem.
Let's assume Im getting list of items and they have column called status, each status is constant and now I need to get translated values so making here relation status=key(constant).
Table with translated values has color which I'd like to use to fill row.
I see in documentation that there is rowClassCallback, but I'd to return inline style like: background-image: $color (for selected status).
Even in this function (rowClassCallback) I can't check value of color because it's in data.
Here in options rowClassCallback is example what I'd to make.
Vue.use(VueTables.ClientTable);
new Vue({
el: "#app",
data: {
columns: ['name', 'code', 'uri'],
data: getData(),
options: {
headings: {
name: 'Country Name',
code: 'Country Code',
uri: 'View Record'
},
editableColumns: ['name'],
sortable: ['name', 'code'],
filterable: ['name', 'code'],
rowClassCallback: row => {
return `background-color: ${this.getProperColor(row.id)}`;
}
}
},
methods: {
getProperColor(id) {
if (id === 245) {
return "#32CD32"
}
}
},
});
function getData() {
return [{
code: "ZW",
name: "Zimbabwe",
created_at: "2015-04-24T01:46:50.459583",
updated_at: "2015-04-24T01:46:50.459593",
uri: "http://api.lobbyfacts.eu/api/1/country/245",
id: 245
}, {
code: "ZM",
name: "Zambia",
created_at: "2015-04-24T01:46:50.457459",
updated_at: "2015-04-24T01:46:50.457468",
uri: "http://api.lobbyfacts.eu/api/1/country/244",
id: 244
}, {
code: "YE",
name: "Yemen",
created_at: "2015-04-24T01:46:50.454731",
updated_at: "2015-04-24T01:46:50.454741",
uri: "http://api.lobbyfacts.eu/api/1/country/243",
id: 243
}, {
code: "EH",
name: "Western Sahara",
created_at: "2015-04-24T01:46:50.452002",
updated_at: "2015-04-24T01:46:50.452011",
uri: "http://api.lobbyfacts.eu/api/1/country/242",
id: 242
}, {
code: "RS",
name: "Serbia",
created_at: "2015-04-24T01:46:50.342496",
updated_at: "2015-04-24T01:46:50.342501",
uri: "http://api.lobbyfacts.eu/api/1/country/196",
id: 196
}];
}
You can use similar method named rowAttributesCallback instead of rowClassCallback to pass your arguments to row.
options: {
editableColumns: ['name'],
sortable: ['name', 'code'],
filterable: ['name', 'code'],
rowAttributesCallback: row => {
return {"style": "color: red"};
}
}

Vue.js - search sort table with an image field

I have a Vue.js component/view that shows the data as follows:
<tbody v-for="item in items">
<tr>
<td width="15%"><img :src="item.image"></td>
<td width="50%">{{item.name}}</td>
<td>{{item.purchasedate | moment(" MMMM Do YYYY") }}</td>
<td>${{item.price}}</td>
<td>Item destination link</td>
</tr>
</tobdy>
The data is powered by a simple axios get call that returns the items array. I need to implement a search and sort function. I have tried several out of box components. Including out of box components available on NPM e.g. vue-good-table, vue-table-component, and grid-component. There are two issues I am facing with these
1. The image cannot be displayed and it only returns the URL
2. Date cannot be formatted - I am using moment.js for that.
I would like to sort by name, price, and purchasedate while displaying the image. Any suggestions?
with vue-good-table,you can set column option like this:
columns: [
{
label: 'photo',
field: 'photo',
html: true
},
{
label: 'Name',
field: 'name',
filterOptions: {
enabled: true,
},
},
{
label: 'Age',
field: 'age',
type: 'number',
},
{
label: 'Created On',
field: 'createdAt',
formatFn: v=>moment(v).format(" MMMM Do YYYY")
},
{
label: 'Percent',
field: 'score',
type: 'percentage',
},
],
and data like this:
rows: [
{ id:1,photo:"/static/logo.png", name:"John", age: 20, createdAt: '201-10-31:9: 35 am',score: 0.03343 },
{ id:2,photo:"/static/logo.png", name:"Jane", age: 24, createdAt: '2011-10-31', score: 0.03343 },
{ id:3,photo:"/static/logo.png", name:"Susan", age: 16, createdAt: '2011-10-30', score: 0.03343 },
{ id:4,photo:"/static/logo.png", name:"Chris", age: 55, createdAt: '2011-10-11', score: 0.03343 },
{ id:5,photo:"/static/logo.png", name:"Dan", age: 40, createdAt: '2011-10-21', score: 0.03343 },
{ id:6,photo:"/static/logo.png", name:"John", age: 20, createdAt: '2011-10-31', score: 0.03343 },
{ id:7,photo:"/static/logo.png", name:"Jane", age: 24, createdAt: '20111031' },
{ id:8,photo:"/static/logo.png", name:"Susan", age: 16, createdAt: '2013-10-31', score: 0.03343 },
].map(o=>({
...o,
photo:`<img width="80" height="80" src="${o.photo}"/>`
}))
and the result is:

How to delete data from DB with the same dates except the biggest date. Rails 3

I have date like:
=> #<Item id: 8749, asin: "B000V2ACH8", created_at: "2014-03-24 00:15:24">
=> #<Item id: 8750, asin: "B000V2ACH8", created_at: "2014-03-24 14:35:24">
=> #<Item id: 8751, asin: "B000V2ACH8", created_at: "2014-03-24 19:35:24">
=> #<Item id: 8752, asin: "B000V2ACH8", created_at: "2014-03-24 17:35:24">
...
=> #<Item id: 8753, asin: "7770V2A888", created_at: "2014-03-24 21:58:24">
=> #<Item id: 8754, asin: "7770V2A888", created_at: "2014-03-24 05:24:24">
...
=> #<Item id: 8755, asin: "7770V2A888", created_at: "2014-03-23 23:58:24">
=> #<Item id: 8756, asin: "7770V2A888", created_at: "2014-03-23 22:58:24">
...
=> #<Item id: 8757, asin: "7770V2A888", created_at: "2014-03-22 19:58:24">
=> #<Item id: 8759, asin: "7770V2A888", created_at: "2014-03-22 10:58:24">
=> #<Item id: 8760, asin: "7770V2A888", created_at: "2014-03-22 23:33:33">
How to get max(create_at) of the same year-month-date & delete rest with the same asin & the same year-month-date(lower than max of that date)?
Want to get values like ⬇
=> #<Item id: 8751, asin: "B000V2ACH8", created_at: "2014-03-24 19:35:24">
=> #<Item id: 8753, asin: "7770V2A888", created_at: "2014-03-24 21:58:24">
=> #<Item id: 8755, asin: "7770V2A888", created_at: "2014-03-23 23:58:24">
=> #<Item id: 8760, asin: "7770V2A888", created_at: "2014-03-22 23:33:33">
group_records = Item.where('id>0') # you can put any conditions inwhere` clause
max_date = group_records.select('max(created_at) as m').first.m
group_records.where('created_at < ?', max_date).delete_all
Using SQL:
DELETE items
FROM items
INNER JOIN (select asin,cast(created_at) created_date, max(created_at) as m
from items group by asin) max_items ON max_items.asin = asin.asin
AND max_items.created_date = cast(items.created_at as DATE)
WHERE items.created_at < max_items.created_at
Definitely not the best answer, but it works for me :)
Example :)
#item.try(:each) do |i|
for j in 0..10
group_records = Item.where(:asin => i.asin, :domain => i.domain, :user_id => i.user_id).where(:created_at => Date.today.beginning_of_day - j.day..Date.today.end_of_day - j.day)
max_date = group_records.select('max(created_at) as m').first.m
group_records.where('created_at != ?', max_date).delete_all
end
end