Rails: JSON URL returning HTML - ruby-on-rails

I have a Rails 4 application, and the URL localhost:3000/resources.json returns and HTML file, instead of JSON. The index.json.jbuilder is generated by Rails scaffold itself. Any idea why the JSON output is going wrong?

Thought I will answer this question myself based on further searches.
HAML prevents template engines to render anything else than HTML
http://denysonique.blogspot.com/2012/03/rails-views-htmlhaml-vs-haml-htmlerb-vs.html

Related

How to allow users to edit dynamic slim page templates in production for rails 4?

Essentially I'm trying to implement a way so that users can edit slim that is stored in the database.
For example they would use the form to create a new page and insert the html for that page in a text field which would be saved in the database. I want to allow them to edit that page in slim. By the way the html stored is slim not plain html.
If I store slim in the database how do I get rails to render the html properly on the client side in production? So in other words would rails automatically do this since the view is being render like so:
views/page/view.html.slim
page.header
page.content
page.footer
or would I have to figure out a way to convert on the fly? I might be making this more complicated then I should but I'm new to this
If I understand you correctly you want to convert the slim to Html and output that in your views.
This is directly from slims doc. This is how it processes slim files and outputs it.
Tilt.new['template.slim'].render(scope)
Slim::Template.new('template.slim', optional_option_hash).render(scope)
Slim::Template.new(optional_option_hash) { source }.render(scope)
so in short
Slim::Template.new(page/view.html.slim).render
put that in a module to make it prettier and I think you're good. You may want to use rails path helper to get the direct link for the view. You may also want to consider figuring out a way to catch the errors in indentation so that your output doesn't bug out in production. Some kind of validation that prevents it from saving if not properly formatted should help.

What is the best way to handle submission of non-utf-8 data in Rails 3 forms?

I am using Rails 3.2.3 and Ruby 1.9.3.
I have a model called called Post that accepts a title and a description.
The front-end of the site receives information submitted through the back-end through an ajax request. When I fill out the form with, let's say
title: foo
content: foobar
and submit it, I am able to view data through the front-end without a problem.
However, whenever I submit non-utf8 data through the form, for example (mind the fancy quotes):
title: foo
content: “foobar”
When I try to render the form I get the following error:
ActionView::Template::Error (incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string))
My .js.erb file looks like this:
$("#my_post").html('<%= escape_javascript(render :partial => 'post') %>');
I realize this is an issue with encoding, but I'm not sure how I should handle it the best way. I thought of several options:
Strip out non-utf8 by using the iconv library -- do this via a before_save filter for every single model in my application
Specifying at the top of the js that the document contains utf-8 (not sure this would work)
Using accept-charset="UTF-8" in my form to force the browser to avoid submission of non-utf-8 content.
I'm not even sure these solutions would help and the most efficient way to do this would be.
Thanks!
I suspect that you're not using the form helpers because you mention the question of adding accept-charset="UTF-8" to your form.
The form helpers will add the accept-charset attribute as well as the snowman parameter which together should ensure you get UTF-8 data from the browser.
See the Rails Form helpers guide.
You need to look carefully to see if
You're actually sending non-UTF-8 data to your app, or
You're sending UTF-8 data, but it is not being recognized by Ruby/Rails
To see which it is, you need to examine the data on the "wire." (What's being sent on the Internet.) Use a peeking tool such as Wireshark or a proxy spy such as Fiddler
Curly quotes can be sent using 8859 or UTF-8.
Recommendation You should set the HTML page to be UTF-8. Any Ajax sent from scripts on the page should then also use UTF-8. See http://www.utf8.com/
Added (Re: comment about how Rails sets form's character encoding)
The issue for Ajax character encoding is how was the page's encoding set. A blog post. So be sure to set the page's UTF-8 encoding in your page template.

Including a .text.erb partial in a .html.erb template? (Invoice)

I'm porting an application to Rails 3.
We're an e-commerce site and naturally we send copies of tax invoices by email. We use plain text, so a .text.erb seems logical.
We also display invoices in an area of the user profile, inside <pre></pre> tags. Is there are way I can share a partial between plain text mailer templates, and views in HTML? If I try to render "shared/invoice" inside my HTML ERB template, it says the partial doesn't exist, and that's because it's a .text.erb partial.
What are my options, without duplicating code?
I haven't tried this in Rails 3, but in Rails 2 you could specify the format of the partial. Might be worth giving it a go on Rails 3.
render :partial => "shared/invoice.text.erb"

rails 3 internationalization / localization - embeddings links in translated text

I need to embed links in my translated texts. I followed this post, but it doesn't seem to work in rails 3 anymore as the html tags don't get rendered properly.
Anyone knows how to get this done in rails 3?
Update:
Apparently, the html tags can be escaped by using the html_safe method. But does anyone know if there's another way to solve this problem without using html_safe?
I would like to avoid unescaping my html tags if possible, b/c I've encountered a situation where I have to pass in a text field into my translation, and I would like to avoid unescaping any strings that are user inputted.
Change {{url}} to %{url} and you should be good to go.
Update
Ok, thanks, that's important information about what "doesn't work" means :) So, you need to call the html_safe method on your call to link_to, eg.
link_to(t("log_in_href"), login_path).html_safe
This will tell Rails to render the HTML, not escaped.

Capture HTML output from the view in RAILS and save to DB

Is there a way to capture the output of my view file after it is rendered?
I basically want to save the full HTML output into a database after the page renders.
You could use render_to_string, then stick it in the db and send the string containing the rendering to the browser. See RoR render_to_string doc.
What I ended up using is the following:
after_filter :save_if_necessary
and what I stored was
self.response.body

Resources