How can I put conditionals in my controller? [closed] - ruby-on-rails

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] ] )

Related

Is it possible to deobfuscate this string of obfuscated Lua Code? [closed]

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 2 years ago.
Improve this question
I have a(nother) string of lua code that's obfuscated. I'm wondering if it's possible to deobfuscate it, or to figure out how it was obfuscated, as I've never encountered anything like it before. This string of code is supposedly the main module for a malicious serverside script executor. Knowing what's inside will help us patch the exploit on our platform. I'm told that it would be easy to decipher by getting the constants, because it's VM based obfuscation, we just need a bit of help getting pointed in the right direction.
The code is rather large, so it's in this pastebin.
pastebin com/dtfzBPZk
Deobfuscating this one looks like it will be a slightly more manual process. As usual, the first things you should do are rename variables to have saner names, and add whitespace and indentation to the code. You can see a start to this at https://pastebin.com/eRTGAbTH. Once you do that, you'll see a pattern of functions like this:
(function(...)
local SynapseXen_116 = "hi xen doesn't work on sk8r please help"
local SynapseXen_092 = SynapseXen_100(38909278, 3932326132)
local SynapseXen_069 = {...}
for SynapseXen_109, SynapseXen_043 in pairs(
SynapseXen_069
) do
local SynapseXen_119
local SynapseXen_097 = type(SynapseXen_043)
if SynapseXen_097 == "number" then
SynapseXen_119 = SynapseXen_043
elseif SynapseXen_097 == "string" then
SynapseXen_119 = SynapseXen_043:len()
elseif SynapseXen_097 == "table" then
SynapseXen_119 = SynapseXen_100(4264903821, 30110892)
end
SynapseXen_092 = SynapseXen_092 + SynapseXen_119
end
SynapseXen_140[1171393165] =
SynapseXen_bit_bxor(
SynapseXen_bit_bxor(2179831066, SynapseXen_092),
SynapseXen_bit_bxor(2132161653, SynapseXen_082)
) -
string.len(SynapseXen_116) -
SynapseXen_139 -
#{
2716917292,
2960928816,
2092744992,
3945961999,
2156388474,
2523828292,
534526172
}
return SynapseXen_140[1171393165]
end)({}, {}, 14275, 107, "iIIllIIlIIilillilI", "i", 5327, 3211, 14382, 14643)
Now you can start to eliminate red herrings. For example, any time you see #{ a bunch of stuff in here }, you can just count the elements in the list, and replace the whole thing with the count. In this case, there's one of those near the end we can replace with the number 7. Next, look at SynapseXen_116. The only place it's used is for its length, so you can substitute that in as well. Now, After that, notice that this is declaring a function and then immediately invoking it, so you can substitute in its arguments. Continue going down that path until you uncover the heart of the Lua-in-Lua VM, and from there, it should be easy to plug in the Base64 at the end, and see what bytecode it decodes to.

Dart Flutter : How to use multiple statements in ternary inside a flutter widget [closed]

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 2 years ago.
Improve this question
Is it possible to write such a line, and within this line I want change a variable value?
opacity: condition == true ? 1, stringName ='Steve' : 0
Not sure what you expect the code to do exactly but my guess
opacity: condition == true ? (){ stringName = 'Steve'; return 0; }() : 0;
You can't have a list of expressions in the true or false part, only one single expression.
I think this is what you wanted
opacity : condition== true ? 1 : stringName == "Steve" ? 0 : 2,
I am not sure you will be able to achieve the best result in a case like this using ternary expressions. Instead, you can create a sperate function that you can bundle in all your conditions and maybe return an appropriate variable as the case may be.
After the you can call the SetState() Function to update your widget.

Heroku infinite loop [closed]

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.

How to get hours from array of datetime attributes? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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.
Improve this question
=> [#<WelcomeCall id: 16, call_at: "2013-11-06 12:00:00">,
#<WelcomeCall id: 17, call_at: "2013-11-06 13:00:00">,
#<WelcomeCall id: 18, call_at: "2013-11-06 17:00:00">,
#<WelcomeCall id: 19, call_at: "2013-11-06 14:00:00">]
I would like to get an array of hours form all these WelcomeCall objects.
=> [12, 13, 17, 14]
WelcomeCall is rails model.
I used for that:
def self.scheduled_hours date
by_date(date).map do |wc|
wc.call_at.hour
end
end
but maybe there is better way?
You want to use map.
Try something like this:
new_array = old_array.map{ |welcome_call| welcome_call.call_at.strftime('%H').to_i }
Assuming that your welcome_call.call_at is a Date. If it's not a Date object, then try:
new_array = old_array.map do |welcome_call|
date = Date.parse(welcome_call.call_at)
date.strftime('%H').to_i
end
You could iterate over the array and parse the call_at datetime object using strftime('%H').to_i.
welcome_calls.map {|w| w.hour}
Parsing the Date to a String and then reparsing the String to a Fixnum is not as fast I think.
I just tried something and it worked, i'm not sure if it's the right way.
var = Model.all
t = var.collect(&:call_at)
hours_arr = []
t.each do |value|
hours_arr << value.time.hour
end

what's the difference between -> and => in rails [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
today I follow the rails guide and run a demo which include a scope as below:
scope :me, =>(keyword){where("title = ?",keyword)}
but it not work,so I change to :
scope :me, ->(keyword){where("title = ?",keyword)}
now it works,so I want to know the the difference between -> and => in rails
but I didn't find the result,so please tell me,thank you.
=> separates the keys from the values in a hashmap literal
-> - new lambda (syntactic sugar)
Examples :
h = { "foo" => "bar" }
l = ->{ "hello" }
l.call # => "hello"
The first is a syntax error. Wherever you read that, it's completely wrong.
The second is commonly known as 'stabby lambda syntax' - its a shortcut for writing:
lambda { |keyword| where('title = ?', keyword) }
More about lambdas in Ruby: http://rubymonk.com/learning/books/1-ruby-primer/chapters/34-lambdas-and-blocks-in-ruby/lessons/77-lambdas-in-ruby

Resources