I'm rendering a header universally across my application in layouts/application.html.erb.
I'd like to make the header not appear in a specific foo.html.erb file.
What's the syntax for un-rendering a universal layout?
EDIT:
The controller for the layout is a devise controller, specifically Sessions Controller.
in your controller you can set the layout to false (or another layout), if false then you need all of the html,head,body tags in your view file
class BarController < ApplicationController
def foo
render :layout => false # render foo.html.erb with no layout
end
end
see section 2.2.11.2 from the rails guides: http://guides.rubyonrails.org/layouts_and_rendering.html
EDIT: including devise layout overrides
in config/initializers/devise.rb
Devise::SessionsController.layout "bar"
Devise::RegistrationsController.layout "foo"
Devise::ConfirmationsController.layout false # never tried this, guessing it would work
Devise::UnlocksController.layout "bar"
Devise::PasswordsController.layout "foo"
also see the wiki post - https://github.com/plataformatec/devise/wiki/How-To:-Create-custom-layouts has at least one other way
Let's say the controller and action that renders the template foo.html.erb is 'things#foo' and the path to this action is things_path. You can wrap the header in conditional tags as follows
<% unless request.path == things_path %>
<% end %>
. There are several ways to achieve this, but here is one.
Related
I have been working on a project in Ruby's Sinatra with ActiveRecord and MySQL and have ran into an error: part of my site is adding a link to the database and I want to show all of the links on the next page but for some reason I can not seem to find out how to express this.
This is what I have so far:
get '/dashboard' do
#site = Site.find_by text: params[:text]
#sites= Site.all
#newsite = Site.new ({:text => :text})
#user = User.find_by_id session[:user_id]
erb :dashboard
end
This is what ERB page looks like:
<% #sites.each do |site| %>
<h4><%=site.text.to_s%></h4>
<% end %>
How would I solve this? Now my form works but it does not show anything after
Your ERB code references a #sites attribute which you don't set in the controller.
If you set #sites in your ruby code to be an array of the Site records you want to show, then you should see something.
In my rails app, html tag , script tag, head tag is created by itself even when I have written nothing in my index.html.erb or the controller method.
I want to output an empty page so that only that text appears which I write myself in controller or view, I don't want title, head, script, link to stylesheets, body and things like that to appear automatically on the view.
You need modify your application layout or create another. Or you may just be looking to render plain: 'Your text'. Either way you should look more into Layouts and Rendering
It's probably a layout that's yielding to your index.html.erb. You can tell a particular controller that you don't want to use a layout by adding this near the top of the controller code, for example in ApplicationController if you wanted to do it for your whole application:
class ApplicationController < ActionController::Base
layout nil
Or if you wanted to use some custom layout (for example named "custom_layout.html.erb") with very little or nothing in it, you could specify in any of your controllers:
class SomeController < ActionController::Base
layout custom_layout
In Controller, with respond_to you can exclude layout
def index
respond_to do |format|
format.html {render :layout => false}
end
end
This will give you just index template contain
I am creating a web app in Ruby on Rails which pulls images from instagram and puts them into an array.
I have a initializer/config/will_paginate_array.rb with the line:
require "will_paginate/array"
My controller looks like this:
def index
result = []
while result.length < 100
//my logic here which adds a bunch of images to the array
result.concat(data)
end
#results = result.paginate(:page => params[:page], :per_page => 13)
end
I am using this line in my html to render the links, but the links are not showing:
<%= will_paginate #results %>
To be clear, when I inspect element the markup is there(and all the pagination links are fully functional), it just isn't rendering/showing up on the page properly for some reason, and I am fairly sure it is not my css overriding anything. The pagination div generated by will_paginate has a height and width of 0. If I manually enter the next page for example localhost:3000?=2 it still advances the page correctly.
Perhaps it is some sort of compatibility issue with will_paginate and arrays?
I have a Publication model with a has_many relationship to Contributors. In the model, I have a method that's meant to create an html-ready by line:
def authors
authors = []
Contributor.where(:publication_id => self.id).each do |author|
authors << "link_to "+author.name+", Contributor.find("+author.id.to_s+")"
end
authors.to_sentence
end
In my view, I have the following line:
by <%= #publication.authors %>
But instead of rendering links, it renders the raw code, such as:
by link_to B, Contributor.find(1)
I've tried patching this by adding .html_safe to the end of #publication.authors, but to no avail. Is there a better way to transfer these links from the model to the view?
You're pushing strings into your authors array. It looks like valid code, so running eval on it should work. (Actually author.name will probably evaluate as an undefined symbol, so scratch that.)
A better way would be to use a has_many :authors, :model => 'Contributor' relationship on your Publication model, and you can bring up your array of Contributor objects by simply calling
#publication.authors
You'd want to iterate over these in your view like so:
<% #publication.authors.each do |author| %>
<%= link_to author.name, author %>
<% end %>
Note also that if you're displaying multiple Publication objects in a view this way, you'll want to use Publication.includes(:authors) in your controller when you're retrieving them to avoid the "N+1" problem.
Now, three lines of code doesn't seem very expensive to repeat, but there are ways to DRY that without violating the MVC pattern and cluttering your model:
Place the code to print a publication's authors into a partial, and call the partial as needed.
Place the code into a helper, include the helper and call the method as needed.
Here's a snippet from the source for to_sentence (you can adapt it for your needs, I think):
case length
when 0
""
when 1
self[0].to_s.dup
when 2
"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
else
"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
end
The full source can be found here.
It looks like you are trying to use haml syntax in your line. Maybe instead of using link_to, use an html hyperlink tag itself?
That being said, why are you having a model return html?
Edit: bdares answered already with what I was trying to say
I'm building a website for my Web Dev class, and I'm stuck on rendering HTML. I want to be able to use a simple form (Pretty much all I have right now is a scaffold for this controller, and I attempted sticking a content_type into my controller, but no progress.) to submit text and have it rendered as HTML. The idea is that, since this class requires a bunch of crap copied out of the book as examples and reference for HTML, maybe I could serve them up in the same way as the blog posts. (All on the same page, using the same layout. The only thing that changes is a content div below the Blog list and the Data (Controller in question) list.
So, in short, my question is: How do I get text fetched from DB to render the html tags rather than displaying as plaintext?
Thank you, and please let me know if supplementary information is necessary.
Cameron
Edit: (Adding code. It's really almost nothing past scaffolding, but, whatevs.)
Also, not sure how the code snippet tool is supposed to work. I hope it folds.
class DatapostsController < ApplicationController
before_filter :header
def header
response.headers['Content-type'] = 'text/html; charset=utf-8'
end
# GET /dataposts
# GET /dataposts.xml
def index
#dataposts = Datapost.all
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #dataposts }
end
end
# GET /dataposts/1
# GET /dataposts/1.xml
def show
#dataposts = Datapost.all
#datapost = Datapost.find(params[:id])
#posts = Post.all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #datapost }
end
end
end
This is the view where it's to be rendered. It's a partial that's called from a content_for that's called by the homepage.
<p>
<small>Post title</small>
<%=h #datapost.title %>
</p>
<hr />
<p>
<%=h #datapost.body %>
</p>
<hr />
<hr />
<%= link_to 'Back', dataposts_path %>
I'll go ahead and push what I have onto prod. server for an idea of what I want the functionality to be like.
http://www.sanarothe.com (~5 minutes after edit)
The h method you're calling here:
<%=h #datapost.body %>
is also known as html_escape - here's the relevant link in the documentation. Remove it and your HTML tags should render appropriately.
You should always display code you get from a user with the h method to prevent cross-site scripting attacks. But if it's code you scraped from a book (or whatever) it should be fine.