Rails delete automatic page content - html

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

Related

Editing comments on show.html.erb rather than redirecting to new edit.html.erb page with Ruby on Rails

Like the title says, I am created the blog application from the Rails guide and I want to be able to edit comments made on articles without redirecting to an edit.html.erb page. I want a div that was previously hidden to appear in the middle of the screen with the edit form on it and the text fields filled with the comment that is being edited. Here is an image of the div that is going to appear in the middle of the screen.
I know how to edit comments by going to edit.html.erb but not in the same page or if this is even possible.
I am not looking for help with the css or html, but if there is a way to do this with js or rails that would be awesome. Thanks in advance!
Edit: So the answer by Satendra gets the partial to show up, but the fields do not contain the original comment that I wanted to edit. How can I get the text-fields to be populated with the comment?
Follow these steps to make edit comment on same page.
Create a partial name _edit.html.erb put your edit.html.erb code there.
Create a div in middle of the screen where you want to render this partial.
<div id="edit_comment" >
</div>
put :remote => true to your edit button link.
# change your path accordingly
<%= link_to edit_comment_path(:comment_id => comment.id), :remote => true %>
The :remote => true is the most important part here, it allows the whole Ajax business in the first place.
In comment_controller.rb inside edit function change respond_to js
def edit
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
end
Create edit.js.erb and put below code there.
$("#edit_comment").html("<%= j render(:partial => 'edit', :locals =>{ :comment => #comment }) %>");
# use comment variable in your partial as it is passed as local.
Its Done.

Rails: Creating HTML in a Model

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

Removing header rendering in specific rails .html.erb file

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.

How do I include a bunch of Rails code from controllers/index.html in public/index.html.erb

I know this isn't very rails-y but I'm trying to do a quick hack so I can record a single date that a user selects into my LunchTime database and ship this app.
When I put my HTML that I'd written before into /views/lunch_times/index.html.erb, it overrides my resetting of all of the HTML attributes, plugging in the rails defaults instead.
As a result, I've dumped all of my HTML into /public/index.html.
I have a div and I tried including this, but it's just showing all of that ruby code when it renders:
<%= class LunchTimesController < ApplicationController
# GET /lunch_times
# GET /lunch_times.json
def index
#lunch_times = LunchTime.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #lunch_times }
end
end %>
Is there a way to hack this into public/index.html? I just need a simple date_select helper that I can plug into my DB, put this on heroku and ship it.
Thanks

(rails) taking from DB and rendering into HTML

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.