(rails) taking from DB and rendering into HTML - 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.

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 delete automatic page content

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

Rails - How do you render the html version of a view in one link, and the json.builder version in another link?

I am fairly new to Ruby on Rails but I have been dabbling at a toy app (Practice Management), for practice.
I was able to render a fullcalendar-rails version of my appointments successfully for one link(using appointments_path, and a .json.jbuilder file). However, I now want a list version of the appointments index in html format but I can't seem to find an answer online.
I still want to use the same path (appointments_path), and the same information but with a different format.
Any help is very much appreciated!
Here is my code:
appointments_controller.rb
def index
#appointments = Appointment.all
respond_to do |format|
format.json
format.html
end
end
index.json.jbuilder
json.array! #appointments do |appointment|
json.start appointment.starts_at
json.title appointment.patient.initials
json.url edit_appointment_url(appointment.id)
end
index.html.erb
<div class="appointments-index" id="appointments-index-body">
<% if current_user.appointments.empty? %>
You have no scheduled appointments.
<% else %>
<div id='appointments' class="appointments-body">
<div>
<% end %>
</div>
After implementing appointments_path(format: :json) to either the link for a simple list or the fullcalendar-rails, I got this:
[{"start":"2014-02-13T14:45:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/112/edit"},{"start":"2014-02-
13T16:00:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/113/edit"}, {"start":"2014-02-13T15:00:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/114/edit"},{"start":"2014-02- 13T15:15:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/115/edit"}, {"start":"2014-02-18T10:15:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/116/edit"},{"start":"2014-02- 13T11:00:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/117/edit"}]
providing the id='appointments' in the after else makes sure that fullcalendar-rails will be applied in this div.
Please let me know if there's any info I could add to make this clearer. Thanks!
You can specify format in url helper, like this:
appointments_path(format: :json)
Hope this helps :)

Rails page has wrong html

I have a link to a page in the same folder as this page on my rails site:
<%= link_to 'Special Access', 'followers/special_access' %>
However, when I go to this page it shows a different page on that url.
<p id="notice"><%= notice %></p>
<div id="sent">
<p>Your request has been sent</p>
<%= link_to 'Home', followers_path %>
</div>
I tried deleting the page that the html is from, but first of all I need that page and it also gives me an error.
I edited the controller to contain:
def special_access
format.html { redirect_to followers/special_access }
format.json { render :json => #post }
end
instead of
def show
but that still didn't solve the problem.
How do I get the right html to show up on the right page?
If you do not define a route for special_access, rails will just assume the the special_acces part in the path is the :id of the route for the show page (as the urls look like followers/:id).
So first off, in your routes.rb, find the resources :followers and replace with the following:
resources :followers do
collection do
get :special_access
end
end
And now you should best always use the rails path helpers, so your link would become
<% link_to 'Special Access', special_access_followers_path %>
Here I was assuming the special access is on the collection of the followers, if it should be on a specific follower (which seems more logical to me, but I have no idea of course), you should write
resources :followers do
member do
get :special_access
end
end
And your link would become
<% link_to 'Special Access', special_access_followers_path(#follower) %>
I am not quite sure what you want to do in your controller action, I hope you just want to render an html page (because redirecting to the same url seems silly, and your syntax is wrong there too).
Hope this helps.

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