Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to convert id into string to use in url
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id")
#t_id = #tutorial_id.to_s
render json: #t_id
Getting this error
S
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Tutorial Json array
{"demotutorials":[{"demotutorials":{"id":50}}]}
there are two things for your problem
if you using where the result is activerecord relation, which can
have couple of records, you must select which row, I gave you
sample first fow to choose first record
#tuturial_id is a activerecord row, so you must choose which column, I gave you sample using #tutorial_id.id
below is sample code:
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id").first
#t_id = #tutorial_id.id.to_s
render json: #t_id
Following code is work for me
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id").first
#t_id = #tutorial_id[0].id.to_s
render json: #t_id
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i have this query in SQL Server
select max(win_votos), win.cdt_id, cdt_nome
from tb_ganhadores_win as win
inner join tb_candidatos_cdt as cdt on cdt.cdt_id = win.cdt_id
group by win.cdt_id,cdt_nome
<--
and i need to create a list 'AspNetMvc' page, like
public ActionResult ListWinners(int? id){
LINQ QUERY HERE
return View('that list');
}
sorry about my english
can anyone help me please?
var res= (from win in dbContext.tb_ganhadores_wins
join cdt in dbContext.tb_candidatos_cdts
on win.cdt_id equals cdt.cdt_id
select new {win,cdt})
.GroupBy(x=>new{Id=x.win.cdt_id,Nome=x.cdt.cdt_nome})
.Select(x=>new
{
Votos=x.Max(z=>z.win.win_votos),
Id=x.Key.Id,
Nome=x.Key.Nome
})
.ToList();
If you use entity framework and your model name is like this you can use some think like this:
var result = from win in tb_ganhadores_win
join cdt in tb_candidatos_cdt on cdt.cdt_id = win.cdt_id
group win by new { win.cdt_id, cdt_nome } into g
select new
{
max = g.Max(win_votos),
win.cdt_id,
cdt_nome
};
and return result.ToList();
note :
"tb_ganhadores_win"
and
"tb_candidatos_cdt"
is your models.Replace it by what you want
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def incoming
sender = params[:From]
body = params[:Body]
#subscription = Subscription.all
twiml = Twilio::TwiML::Response.new do |r|
#subscription.each do |subs|
if (("+1"+(subs.customer.phone_number.to_s)) == sender) && (body.downcase == "unfollow")
r.Message "You are unsubscribed."
subs.destroy
elsif ("+1"+(subs.customer.phone_number.to_s)) == sender)
r.Message "I don't know that command."
else
end
end
end
render xml: twiml.text
end
when I try to deploy the code above to heroku, heroku app crashes.
It works well without this part of the code.
I looked at the heroku log and looks like its making a infinite loop in this method.
how can I make this into non-infinite loop?
problem solved. it was that extra parenthesis.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am really new about Ruby development. I am trying to create a json file with strings. My json file like below. Can you help to me
{
"App":{
"properties":{"color":"red"},
"screens":[
{"id":"page1", "properties":{"color":"red"}, "elements":[
{"type":"txtbox", "properties":{"color":"red"}},
{"type":"button", "properties":{"color":"red"}}
]
},
{"id":"page2", "properties":{"color":"red"}, "elements":[
{"type":"txtbox", "properties":{"color":"red"}},
{"type":"button", "properties":{"color":"red"}}
]
}
]
}
}
You can parse JSON with ruby from a hash:
require 'json'
my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"
Or generate it from a hash:
require 'json'
my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
Have a look at the JSON documentation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a method that I can to push to a JSON call. The function looks like:
def my_function(name, text , id_person, id_company)
end
What I want is to get:
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
Is there any easy way to do this?
The question is a bit unclear, but let me try with few answer possibilities
First of all this is not a JSON format
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
There should not be double quote (") before semicolon (:)
obj = {
my_function:
{
name: "names_value",
text: "text_value",
id_person: "id_person_value",
id_company: "id_company_value"
}
}
If you want to get each of the data in that "JSON" format, you could do this
name = obj['my_function']['name'] #name_value
text = obj['my_function']['text'] #text_value
and so on...
But if you want to make a string data into JSON format, you could do this
def my_function(name_value, text_value, id_person_value, id_company_value)
{
my_function:
{
name: name_value,
text: text_value,
id_person: id_person_value,
id_company: id_company_value
}
}.to_json
end
I hope it's answering your question...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm having problems when I'm trying to put a condition in params in my controller. This code is inside my controller:
if params[:example] == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
Is this code correct? How can I put a conditional in params controller?
You code looks okay. Unfortunately you do not say what problem you have. The only thing I see that may fail is the condition. If you pass some value into your params they are not typecasted. Therefore I guess you should use to_i in your condition:
if params[:example].to_i == 1
...
Try with
#table = Model.where((params[:example] == 1 ? 'column_table= ?' : 'column_table2 = ?'), params[:example]) unless params[:example].blank?
Here is code. You have save params[:example] in one variable.and use your coditions.
if params[:example].present?
#example = params[:example]
if #example == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
You can also send the column name along with its value and avoid ifs
#table = Model.find(:all,:conditions=>["#{params[:col]} = ?", params[:example] ] )