Getting a little bit confused as to why these whitespaces are being taken literally.
If you take a look at the image below you'll see a bunch of rectangles. The top rectangle on the left should actualy align with the bar on the right but it doesn't because of a line break.
The rails code for this layout looks like this...
In the layout:
<div id = "left_bar"><%=yield(:left_bar)%></div>
<div id = "main_content"><%=yield%></div>
And in the actual template:
<div class = "set_area images current" id = "set_area_<%= #image_set.id %>">
<%= render('images/images', :images => #image_set.images) %>
<div class = "clear"></div>
</div>
<% content_for :left_bar do %>
<% for set in #image_sets %>
<%= render("set_tab", :set => set, :is_current_tab => set.eql?(#image_set))%>
<% end %>
<% end %>
EDIT: Above code has slightly been simplified for brevity.
UPDATE: When I replaced the line to render the partial:
<%= render("set_tab", :set => set, :is_current_tab => set.eql?(#image_set))%>
with
<p>Hello</p>
The white-spaces didn't show up.
I tracked down the issue to the "set_tab" partial. I started removing and re-adding in lines again.
I believe this went wrong because there were some mixed encodings. I mostly work on linux machine but I did some editing on a windows machine earlier in the day.
If anybody else find their view acting up like this throw out your indentation and redo it.
Related
I recently began working on a documentation site using Ruby on Rails and Turbo (repository is here, and the website is here), and I'm having a slight issue with navigation.
When I press the links in the left sidebar, the content on the right side of the screen is correctly replaced. Here's some relevant code -
articles.html.erb
<div class='docs-split'>
<div class='sidebar'>
<div>
...
<div class='links padding-top'>
<%= link_to render(:partial => 'shared/primary_navigation_link', locals: { title: "Getting Started", path: "getting-started" }), 'getting-started' %>
<%= link_to render(:partial => 'shared/primary_navigation_link', locals: { title: "Types of Charts", path: "types-of-charts" }), 'types-of-charts' %>
</div>
</div>
</div>
<div class='content'>
<%= turbo_frame_tag "article", src: article_path(params[:title] ? params[:title] : "getting-started") do %>
<% end %>
</div>
</div>
articles_controller.rb
require 'github/markup'
class ArticlesController < ApplicationController
def index
end
def open_article
title = Rails.root.to_s + "/app/markdown/" + params[:title] + ".md"
#content = ActionController::Base.render inline: GitHub::Markup.render(title, File.read(title))
end
end
open_article.html.erb
<%= turbo_frame_tag "article" do %>
<%= #content %>
<% end %>
routes.rb
Rails.application.routes.draw do
root :to => redirect('/articles/getting-started')
get 'articles/:title', to: 'articles#index'
# Embedded article GET route
get ':title', to: 'articles#open_article', as: 'article'
end
What I expect to occur is that only the content inside the Turbo tag will change while the rest of the HTMl remains the same and the navigation link styles will transition as specified by my CSS. However, what seems to occur instead is that all of the HTML is reloaded, which means the CSS transitions that are supposed to happen when navigation links are clicked don't happen and the Turbo tag content appears to blink twice.
I'm new to Rails and Hotwire/Turbo, so any help would be appreciated. Thank you!
I'm working in a ruby on rails project but since I'm a frontend developer I've got no knowledge on the ruby on rails way of doing things.
The problem I'm facing is that I want to pass a parameter to another file which I can then use to load the specific content, but I'm not sure how to do this or if it's even possible.
file 1:
<%= render "partials/lightbox/lightbox", card: 'new-card' %>
Then I want this parameter to be used in file 2 :
<div class="site-overlay">
<div class="lightbox lightbox--medium light-theme">
<%= render "partials/lightbox/content/{myparameter}" %>
</div>
</div>
Which will render the lightbox with the content I passed from file 1.
How can I pass a variable to the render in file 2 from file 1?
In rails locals is used for passing vaiable
<%= render "partial_name", :locals => { :variable_name => variable} %>
You can change it to your own data. Hope this will work for you.
Its easy
<%= render partial: 'path/to/partial/file2', locals: {var2: var1} %>
Above code will pass var1 from partial file1 to partial file2 and it will be accessible in file2 partial as var2
Remember partials are named as _filename but when using render you write without underscore.
I hope this helps
You should consider to use partials:
In index.html.erb
<h1>New zone</h1>
<%= render partial: "lightbox", locals: {zone: #zone} %>
Inside same directory you should user a file '_lightbox.html.erb'
Inside this file you can render another partial:
<h2>Inside new zone</h2>
<%= render partial: "lightbox2", locals: {var: zone} %>
since rails4 you can omit partial and locals
<%= render 'path/to/partial/file2', var2: var1 %>
I got the following lines in my view:
<p><%= #runnings_past.map do |f| %>
<%= f.title %>
<% end %>
</p>
which works just fine in the console --> output:
2.1.2 :076 > #runnings_past.map do |f|
2.1.2 :077 > f.title
2.1.2 :078?> end
=> ["Murtenlauf"]
2.1.2 :079 >
But when I use it in the view as seen above, I get an expression like this:
Murtenlauf ["\n"]
Where does the ["\n"] come from?
You are using the wrong iterator. You want to use each instead of map. Map creates a new array where each element is the result of each iteration in the block. While each simply calls the given block once for each element in the array.
In addition, as pointed out by #JTG, you want to remove the =. You only need = for showing output. You don't need it for logic.
<%= #runnings_past.map do |f| %>
<%= f.title %>
<% end %>
Should actually be
<% #runnings_past.each do |f| %>
<%= f.title %>
<% end %>
Notice the lack of = (in addition, I changed map to each because you're just iterating through the array)
<%= means evaluate the code and insert the result into the html structure. So what you were doing was evaluating the .map and putting the result into your html. So you would iterate through the #runnings_past code, place the f.first into the html (which is what you want) but then when you were done with that, you were placing the result of the mapping (which apparently was an array with a string return character) into the html afterwards.
I am new to rails and still working on fundamentals, so any tips and advice are greatly appreciated!
I've made a functional contact page on a website, and I would like to be able to render the form and functionality of inside a tab on a different page. I have the following code in the static_pages folder in my views folder:
<div id="tabs">
<ul>
<li>About</li>
<li>Contact</li>
</ul>
<div id="tabs-1">
<%= render "shared/about"%>
</div>
<div id="tabs-5">
<%= render "contact/new"%>
</div>
where <%= render "contact/new"%> corresponds to the new.html.erb file in contact in views.
the relevant code inside my routes.rb is
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
and here is my controller:
class ContactController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.contact_pr(#message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
However, It seems that whatever I try I still am unable to render the form. I apologize if this is a very simple question, and I am mostly interested in getting better at fundamentals in Ruby on Rails so any advice is appreciated! Thanks!
If you want to render this form in other templates (and you do), you should extract it to partial named for example: _form.html.erb.
Then, you should put
<%= render 'contact/form' %>
everywhere you need this partial in view (including new.html.erb template).
When you are done with this, you should leave
render :new
in controller, as it works a little bit different than in view and it renders appropriate action template instead of partial.
Of course, you need to set #message variable everywhere you need this partial. Or, you can put in your partial:
<%= form_for(#message || Message.new) do |f| %>
More info about layouts and rendering here.
I have the following code:
<%== "From:<br>" %>
<% if flag %>
<%= link_to user.full_name, user_path(:user => { :user_id => user_id }) %>
<% else %>
.
.
.
This works fine, except it outputs a newline and 6 tabs before the link.
From:<br>
<a href="/user?...
This turns into 1 space on the HTML page, breaking the left alignment of the text.
To:
Joe
Brown
Why is it outputting a newline and 6 tabs? Is there a way to suprress this behavior? Is there a CSS solution to create a <span> that will not display tabs as whitespace on the HTML page?
Working in rails 3.07.
Rewrite your code this way:
<%= "From:<br>#{link_to(user.full_name, user_path(:user => { :user_id => user_id })) if flag}" %>
I fixed it in CSS. I think this is a little more robust and allows more flexibility to make Rails code more readable.
Tested on Chrome, Firefox and IE 8 (on Windows).
user_name a {
display:block;
margin:0;
}
By the way, the HTML code is in a td.