What is the purpose of '!= render'? [duplicate] - ruby-on-rails

This question already has answers here:
HAML - what does the "!=" operator do?
(3 answers)
Closed 4 years ago.
I discovered the following statement in a haml template:
!= render partial: 'path/to/partial'
What is the purpose of != in this context?

It is used for Unescaping HTML.
example:
= "<Hello>" will give &ltg;Hello>
but now using
!= "<Hello>" will give <Hello>

Related

possible to do this as a one-liner in Ruby or Rails [duplicate]

This question already has answers here:
ruby using the "&:methodname" shortcut from array.map(&:methodname) for hash key strings rather than methodname
(3 answers)
Closed 3 years ago.
I have a rails app that does a lot of JSON parsing (ie using strings as keys rather than symbols).
I have the following code:
ad_source_ids = []
logged_one['migrated'].each { |mig| ad_source_ids << mig['id'] }
I'd like to do
ad_source_ids = logged_one['migrated'].map(&:id)
but don't think I can. What is an alternative? I'd like to removed the ad_source_ids tmp variable.
You're almost there. Try this:
ad_source_ids = logged_one['migrated'].collect { |mig| mig['id'] }

RAILS range make reverse [duplicate]

This question already has answers here:
Is there a reason that we cannot iterate on "reverse Range" in ruby?
(12 answers)
Closed 7 years ago.
options_for_select(1920..2015)
Howa can get this range in reverse type, 2015..1920 doesn't work,
I think my question is pretty understandable, but this form wants me to write more, so excuse me)))Please help
This produces the options in reverse order:
options_for_select((1920..2015).to_a.reverse)
=> "<option value=\"2015\">2015</option>\n<option value=\"2014\">2014</option>\n<option value=\"2013\">2013</option>\n<option value=\"2012\">2012</option>\n<option value=\"2011\">2011</option>\n<option value=\"2010\">2010</option>\n<option value=\"2009\">2009</option>\n<option value=\"2008\">2008</option>\n<option value=\"2007\">2007</option>\n<option value=\"2006\">2006</option>\n<option value=\"2005\">2005</option>\n<option value=\"2004\">2004</option>\n<option value=\"2003\">2003</option>\n<option value=\"2002\">2002</option>\n<option value=\"2001\">2001</option>\n<option value=\"2000\">2000</option>\n<option value=\"1999\">1999</option>\n<option value=\"1998\">1998</option>\n<option value=\"1997\">1997</option>\n<option value=\"1996\">1996</option>\n<option value=\"1995\">1995</option>\n<option value=\"1994\">1994</option>\n<option value=\"1993\">1993</option>\n<option value=\"1992\">1992</option>\n<option value=\"1991\">1991</option>\n<option value=\"1990\">1990</option>\n<option value=\"1989\">1989</option>\n<option value=\"1988\">1988</option>\n<option value=\"1987\">1987</option>\n<option value=\"1986\">1986</option>\n<option value=\"1985\">1985</option>\n<option value=\"1984\">1984</option>\n<option value=\"1983\">1983</option>\n<option value=\"1982\">1982</option>\n<option value=\"1981\">1981</option>\n<option value=\"1980\">1980</option>\n<option value=\"1979\">1979</option>\n<option value=\"1978\">1978</option>\n<option value=\"1977\">1977</option>\n<option value=\"1976\">1976</option>\n<option value=\"1975\">1975</option>\n<option value=\"1974\">1974</option>\n<option value=\"1973\">1973</option>\n<option value=\"1972\">1972</option>\n<option value=\"1971\">1971</option>\n<option value=\"1970\">1970</option>\n<option value=\"1969\">1969</option>\n<option value=\"1968\">1968</option>\n<option value=\"1967\">1967</option>\n<option value=\"1966\">1966</option>\n<option value=\"1965\">1965</option>\n<option value=\"1964\">1964</option>\n<option value=\"1963\">1963</option>\n<option value=\"1962\">1962</option>\n<option value=\"1961\">1961</option>\n<option value=\"1960\">1960</option>\n<option value=\"1959\">1959</option>\n<option value=\"1958\">1958</option>\n<option value=\"1957\">1957</option>\n<option value=\"1956\">1956</option>\n<option value=\"1955\">1955</option>\n<option value=\"1954\">1954</option>\n<option value=\"1953\">1953</option>\n<option value=\"1952\">1952</option>\n<option value=\"1951\">1951</option>\n<option value=\"1950\">1950</option>\n<option value=\"1949\">1949</option>\n<option value=\"1948\">1948</option>\n<option value=\"1947\">1947</option>\n<option value=\"1946\">1946</option>\n<option value=\"1945\">1945</option>\n<option value=\"1944\">1944</option>\n<option value=\"1943\">1943</option>\n<option value=\"1942\">1942</option>\n<option value=\"1941\">1941</option>\n<option value=\"1940\">1940</option>\n<option value=\"1939\">1939</option>\n<option value=\"1938\">1938</option>\n<option value=\"1937\">1937</option>\n<option value=\"1936\">1936</option>\n<option value=\"1935\">1935</option>\n<option value=\"1934\">1934</option>\n<option value=\"1933\">1933</option>\n<option value=\"1932\">1932</option>\n<option value=\"1931\">1931</option>\n<option value=\"1930\">1930</option>\n<option value=\"1929\">1929</option>\n<option value=\"1928\">1928</option>\n<option value=\"1927\">1927</option>\n<option value=\"1926\">1926</option>\n<option value=\"1925\">1925</option>\n<option value=\"1924\">1924</option>\n<option value=\"1923\">1923</option>\n<option value=\"1922\">1922</option>\n<option value=\"1921\">1921</option>\n<option value=\"1920\">1920</option>"
Reversing a range doesn't really make sense. You could use options_for_select(2015.downto(1920)), as downto returns an Enumerator which is a valid parameter for options_for_select.

Rails where query with or [duplicate]

This question already has answers here:
Rails ActiveRecord where or clause
(4 answers)
Closed 9 years ago.
i have the following query in my app:
Page.where(published: false).where("published_at > current_date").where("publication_end IS NULL OR publication_end < current_date")
Now i want to change this where to or, I was trying:
Page.where((published: false) || ("published_at > current_date"))
but it not working. Is there any way to do it?
This should work:
Page.where('published = ? OR published_at > current_date', false)

How can I put conditionals in my controller? [closed]

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

Ternary Operator in Razor View [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to use ternary operator in razor (specifically on HTML attributes)?
I am trying to do the following but its erroring so I'm obviously doing something wrong with the Razor syntax:
<td>#{item.Licence.MachineId != null ? #:"TB Master" : #:"HandHeld"} </td>
The following should work:
<td>#(item.Licence.MachineId != null ? "TB Master" : "HandHeld")</td>
Try this
<td>#(item.Licence.MachineId != null ? "TB Master" : "HandHeld")</td>

Resources