I have a simple eRubis (*.html.erb) document, and want to "copy" various elements throughout a page. Whenever I use simple Ruby statements like this:
<%= 3.times do %> ... <% end %> with multiple "times" statements within that to copy more elements returns either errors or horribly rendered elements. What is the best way to "copy" multiple elements throughout a eRubis page using Ruby statements?
One approach I use in RoR is content_for and yield. I store my element(s) in a content_for and then I litter yields around wherever I want that element:
<% content_for :some_elements do %>
<divs or whatever />
<and maybe some things too />
<%= even some more erb %>
<% end %>
<%= yield :some_elements %>
<%= yield :some_elements %>
<%= yield :some_elements %>
Those yields can go anywhere, you could even have the content_for in your layout file and put those yields in any view or partial as many times as you want.
If you want to simply mock up a list or something, times works perfectly well if you use it correctly:
<ul>
<% 10.times do |i| %>
<li id="item_#{i}">
content in item <%= i %>
</li>
<% end %>
</ul>
But of course the times approach requires that all the elements be in the same spot. That's the advantage of the content_for/yield way, you can place the yields where ever you want. You can even put a yield inside a times:
<% 10.times do |i| %>
<%= yield :some_elements %>
<% end %>
But that's just crazy. Anyway, have fun.
Related
I have a new.html.erb and a _form.html.erb
How do you align a button and a link if the back link is in the new.html.erb file while, a submit button would be in the _form.html.erb?
new.html.erb
<%= render 'form' %>
<%= link_to 'Back', project_path %>
_form.html.erb
<%= simple_form_for(#trip) do |f| %>
<%= f.input :project_name, class: 'form-control' %>
<div class="top-buffer">
<%= f.button :submit, class: "btn btn-primary pull-right" %>
</div>
<% end %>
Is there a rails way to align the back and the button together? What would be the conventional way?
Should I just simply put the back button in the _form.html.erb? Will this affect anything in the long run?
_form.html.erb is what is called a partial. Partials allow you to remove unnecessary duplication of code. They help breaking the rendering process into more manageable chunks without changing functionality.
To answer all of your questions, ask yourself why you are using a partial. If this is to remove duplication then go ahead and move the back button inside _form.html.erb.
Now, if you could provide some css we could actually help you to center those buttons.
UPDATE
I have just realised you are using bootstrap. In this case try updating your link to this...
<%= link_to 'Back', project_path, class: "btn btn-primary pull-left" %>
and see if it solves your problem.
This is what I got ...
... and it seems more or less aligned to me.
I have been trying to find something about embedding HTML code inside a partial argument for days but I have not found anything so I'm guessing it isn't possible. But it seems like it should be.
I have a static page in my Rails app which has a lot of sections and each section can have subsections. I could just make the entire page just plain HTML. But I didn't want to repeat the same formatting over and over in case I want to change classes or something else.
So I have the following _section.html.erb partial file:
<div class="row">
<h4><%= heading %></h4>
<% subsections.each do |section| %>
<% if section[:header] %>
<h5 class="primary-text"><%= section[:header] %></h5>
<% end %>
<p><%= section[:body] %>
<% end %>
</div>
That works fine. But what if I want to include a link to a page or an email inside one of the subsections? It doesn't work just by passing it in as part of the quotes text. It shows the actual HTML tags.
Is there a real way to do this or should I give up and just write plain HTML with repeated section formatting?
You mark your text as html_safe. For example:
<%= section[:header].html_safe %>
But I would suggest using sanitize method because of security resonons:
<%= sanitize section[:header] %>
Probably sometimes you will want to configure sanitize method. Here you can read how to do this:
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
You can read more about security here:
http://guides.rubyonrails.org/security.html#cross-site-scripting-xss
very straight forward..
how do I add a class to something like this
<h2><%= guide.title %></h2>
which is just displaying text?
You have to wrap it within some container:
<div class="my_class"><%= guide.title %></div>
The container you'll use depends on the context given text is to be used.
Update:
Since the text is already wrapped in <h2> you can do:
<h2 class='my_class'><%= guide.title %></h2>
Another update:
If you wan to minimize the amount of pure html in your view, you can always do:
<%= content_tag :h2, class: 'my_class' do %>
<%= guide.title %>
<% end %>
I'm currently developing a web app and I have a slight problem. I have a redirect_to root_path statement in my view's controller that is called if a logic statement returns true. On the page being redirected to, I have a stylesheet_link_tag in the layout view. When I pull up the page manually, this tag loads correctly. However, after the redirect_to statement is used and this page is loaded from it, the stylesheet tag is no longer there. It still loads the application.css file and my application.js file without anyone issues but the pagename.css link tag doesn't even show up.
And here's how the page displays:
Then once I refresh:
PLEASE help me! I would love to know why this is happening and how to fix it. Thank you in advance!
Here are all of the relative code bits:
users_controller.rb
...
def logout
cookies[:logged_in] = false
cookies[:user_id] = 0
redirect_to root_path # Tried using root_url also; didn't fix the problem.
end
...
homepage/index.html.erb (root_path)
A bunch of HTML. Not the problem.
layouts/homepage.html.erb
<!DOCTYPE html>
<html>
<head>
<title>...</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'homepage' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="header_container">
<%= link_to "#{image_tag 'logo.png'}".html_safe, root_path %>
<div class="login tab">
<span>
Login
<%= image_tag "down_arrow.png" %>
</span>
<%= form_tag login_path, :method => 'post' do %>
<% end %>
</div>
<div class="register tab">
<span>
<%= link_to "Register", register_path, "data-ajax" => "false", :rel => false %>
</span>
</div>
</div>
<div id="main_container">
<%= yield %>
</div>
</body>
</html>
I had to add the homepage.css asset to config/locales/application.rb, so here's that:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module Labs
class Application < Rails::Application
config.assets.precompile += %w( homepage.css )
config.assets.precompile += %w( dropdown.js )
end
end
The problem can be two-fold:
--
Turbolinks
Firstly, you may have an issue with Turbolinks
Turbolinks is notorious for preventing styling & javascript changes on pages; as it basically just refreshes the <body> tag of your page (keeping the <head> intact). Although this is mainly for if you're traversing pages through link clicks (I'm not sure about redirects), you basically need to make sure any styling changes can occur even with Turbolinks enabled.
To do this, you need to ensure you're loading without calling Turbolinks. To do this, you should use the data no-turbolink tag, as follows:
<%= link_to "link", link_path, data: { no_turbolink: true } %>
This will ensure you don't call turbolinks when you click that particular link, ensuring a "naked" refresh if you will.
--
Redirect
The redirect method might be the cause of the issue you're facing.
When you mentioned:
The redirect_to statement is used and this page is loaded from it, the stylesheet tag is no longer there. It still loads the application.css file and my application.js file without anyone issues but the pagename.css link tag doesn't even show up.
The way to fix this is to ensure your layout includes the correct stylesheets. You've included your homepage.html.erb code - I would be eager to say that if pagename.css isn't loading, it basically means you're not calling the file correctly.
I would do this:
<%= stylesheet_link_tag controller_name %>
This will allow you call stylesheets per controller (using the controller_name helper method) - allowing you much broader control of what you're loading on the page
My goal is to customize the tags for blog posts in the Refinery CMS. By default they are not styled. I added some styling just to test that I was making changes to the right areas (yes I know it is ugly).
But my goal intent is to get them in a format similar to what I have on my own blog.
Refinery is adding a comma after each tag. What should I modify in the html or css to not add the commas?
app/views/refinery/blog/shared/_tags.html.erb
<% if #tags.any? %>
<h2>Tag Cloud</h2>
<nav id='tags'>
<% tag_cloud(#tags, %w(tag1 tag2 tag3 tag4)) do |tag, css_class| %>
<%= link_to tag.name, refinery.blog_tagged_posts_path(tag.id, tag.name.parameterize), :class => css_class %>
<% end %>
</nav>
<% end %>
The tag_cloud method is a method found in the acts-as-taggable-on library that the refinerycms-blog extension uses. More information can be found out about it in the readme for the library.