I have a variable tag <%test_variable%> that was from a resultset.
I want to use this <%=test_variable%> to redirect to a link, say
http://www.helloworld.someurl.com/testUrl?somevariable=<%=test_variable%>&test=ok
How can I do this in the <% %> tag? For example,
<%=test_variable%>
<%
' I want to redirect the url with the above tag, something like:
Response.Redirect(" http://www.helloworld.someurl.com/testUrl?somevariable=<%=test_variable%>&test=ok")
%>
But I don't think we can have "nested tags", right?
I'm pretty new to ASP.
<%= %> is shorthand for <% Response.Write(" ... "); %>:
http://www.helloworld.someurl.com/testUrl?somevariable=<%=test_variable%>&test=ok
After your clarification:
<%
Response.Redirect("http://www.helloworld.someurl.com/testUrl?somevariable="
+ test_variable
+ "&test=ok");
%>
Your code should be
Response.Redirect("http://www.helloworld.someurl.com/testUrl?somevariable=" & test_variable & "&test=ok")
Thanks all for your suggestions...
I've decided to use this instead:
<script type="text/javascript">
window.location = "http://www.helloworld.someurl.com/testUrl?somevariable="+<%=test_variable%>+"&test=ok"
</script>
Related
I want to use the current user's through my coffee script. But if I try to use current_user in my CoffeeScript, I get the undefined variable current_user in console of browser.
Is there any way to access it in coffee script? What I exactly wish to do is as follows:
current_user.updated_at < Date.now().getTime()
I need it to implement this in my Rails app.
For raw javascript + erb
<script type="text/javascript">
var my_foo = <%= some_ruby_expression %>
</script>
e.g.
<script type="text/javascript">
var user_name = "<%= current_user.name %>";
</script>
except var keyword, this is a valid coffeescript code, I believe.
Btw, is it really what you are asking for? - I don't know -))
UPDATE
current_user is a ruby/active_record object. You might assign it to a javascript variable, but can't use as you did in ruby.
But below snippet might give you some idea. I've created a rails project, and scaffolded a page model.
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #page.title %>
</p>
<p>
<strong>Email:</strong>
<%= #page.email %>
</p>
<p>
<strong>Comments:</strong>
<%= #page.comments %>
</p>
<script type="text/javascript">
var mypage = '<%= raw #page.to_json %>'; // attention to single quotes
console.log(mypage);
</script>
<%= link_to 'Edit', edit_page_path(#page) %> |
<%= link_to 'Back', pages_path %>
Then you can parse and use it as an ordinary javascript object. Below picture show, how I did it.
UPDATE #2
do it in coffeescript file.
window.onload = ->
myvar = '<%= raw #page.to_json %>'
myvarAsObj = JSON.parse(myvar)
do_something_with myvarAsObj
return
Coffee script is turned into javascript and runs on the browser. What you want to do is render current_user.updated_at into a node in HTML, then use coffee script to extract that value.
Javascript files are (more or less) static files which will be executed by the browser. I don't know if you ever deployed an rails app in production. You'll run the command bin/rake assets:precompile which will generate one (or more) massive javascript file(s). So how would you find out the current user at this time? there's no HTTP request at all, so how could there be a user?
So to do this, you could either write the current user to a HTML node and then read it again with javascript (coffeescript) (which in my opinion is the best way) or you could print a script tag into your view containing what you want from your user.
I have this statement in application.html.erb:
<%= yield(:javascript) %>
and I have the following Javascript, which should respond to the yield:
<% content_for : javascript do %>
<script type="type/javascript">
$(function() {
call_to_function();
});
</script>
<% end %>
If I place the above block in home/index.html.erb, it works alright, but I want to somehow make it available to application.html.erb all the time, not just when I call home#index.
How can I achieve this, any pointers would be much appreciated?
Instead of using
<%= yield(:javascript) %>
you can directly put your java script to application.html.erb in app/views/layout to share code throw out all the pages.
There is no need to use yield when you have same contents through out. Yield is used when we have code specific to each page is different, from each page we call yield to actually replace the content of yield block.
So, replace <%= yield(:javascript) %> by
<script type="type/javascript">
$(function() {
call_to_function();
});
to appear in all your pages.
I have an array of Strings containing unsave content (user input).
I want to join these Strings in my template, separated by <br />.
I tried:
somearray.join("<br />")
But this will also escape the sparator.
Is there a workaround, keeping in mind that the content of the array absolutely must be escaped?
raw and h provide ways to apply this default behavior selectively.
<%= raw user_values_array.map {|user_value| h user_value }.join('<br />') %>
Better still, Rails 3.1 introduced safe_join(array, sep) for this purpose. Used with html_safe it can do what you need.
<%= safe_join(user_values_array, "<br />".html_safe) %>
Documentation
Is there a reason it has to be a <br /> tag? Could you use a list instead?
<ul>
<% somearray.each do |item| %>
<%= content_tag :li, item %>
<% end %>
</ul>
Have you tried this?
raw somearray.join("<br />")
Nowadays, the state-of-the-art way to solve this is:
# Inside a view:
safe_join(somearray, '<br />')
# From somewhere else, given the current controller:
controller.helpers.safe_join(somearray, '<br />')
Here is my code, I'm trying to display a list of links to a bboy's crews in sentence form with .to_sentence
<span class="affiliation">
<% if(#bboy.crews.count > 0)%>
<span><%= if(#bboy.crews.size > 1) then "Crew".pluralize else "Crew" end %>:</span>
<%= #bboy.crews.collect{|c| link_to c.name, c}.to_sentence %>
<% else %>
<em>Independent</em>
<% end %>
</span>
The output I get is the correct links but it displays as:
Hustle Kidz and Knuckleheads Cali
rather than:
Hustle Kidz and
Knuckleheads
Cali
with the html escaped, rather than the desired links.
Am I missing something? I've tried CGI.unescapeHTML and a few others but am getting lost...
Rails 3 now automatically escapes everything, in order to output raw HTML use this:
<%= some_string.html_safe %>
or this:
<%= raw #some_html_string %>
Thanks to macek for a hint.
For additional details: http://markconnell.co.uk/posts/2010/02/rails-3-html-escaping
You can (and should) you the raw method
<%= raw #some_html_string %>
I agree with Kleber S, you should move this into a helper, because that's a lot of logic for a view
def crews_description(crews)
if crews.empty?
content_tag('em', 'Independent')
else
label = "Crew"
label = label.pluralize if crews.size > 1
crews_links = crews.map {|crew| link_to(h(crew.name), crew)}.to_sentence
content_tag('span', label) + crews_links.html_safe
end
end
and in your view:
<span class="affiliation">
<%= crews_description(#bboy.crews)
</span>
I recommend you move this block of code to an helper and then use the .html_safe method to obtain the expected results.
I'm new to Ruby and Rails and I have a simple controller that shows an item from the database in a default view. When it is displaying in HTML it is outputting <p> tags along with the text content. Is there a way to prevent this from happening? I suppose if there isn't, is there at least a way to set the default css class for the same output in a statement such as this:
<% #Items.each do |i| %>
<%= i.itemname %>
<div class="menu_body">
Link-1
</div>
<% end %>
So the problem is with the <%= i.itemname %> part. Is there a way to stop it from wrapping it in its own <p> tags? Or set the css class for the output?
Thanks!
You need to enclose it with the HTML tag of your choice. Also if required you can escape bad code by using <%=h i.itemname %> Example:
<% #Items.each do |i| %>
<div><%=h i.itemname %></div>
<div class="menu_body">
Link-1
</div>
<% end %>
Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.
You canchange the public/stylesheets/scaffold.css if you want.
Or if you want to change it for a single page say items/index.html.erb
<style>
p{
/* your style here *?
}
</style>