why my rails page always show html heads? - ruby-on-rails

in my data_feed controller and bar action, there's only one line in bar.html.erb:
[1,2,3,4]
My expected output by requesting http://localhost:3000/data_feed/bar is just one line I wrote above.
However, rails help me add some html heads into that page, as below:
<!DOCTYPE html>
<html>
<head>
<title>Guustock</title>
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/charts.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/data_feed.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/show.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/charts.js?body=1" type="text/javascript"></script>
<script src="/assets/data_feed.js?body=1" type="text/javascript"></script>
<script src="/assets/show.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="lhHWCW82xtqQ8fmgBZemDggL2DGJe+4chXM8MY1LKvs=" name="csrf-token" />
</head>
<body>
[1,2,3,4]
How can I avoid rails doing this?
Thanks!
Env:
linux mint 11.0
ruby 1.9.3 with rvm
rails 3.2.1
EDIT:
Actually I want to provide the json format data in this page as an ajax data source. So these heads will not helpful but break the data.

You're probably rendering within the layout (a view template, which lives in app/views/layouts/), which is the default. If you want to render an action without the layout, use something like
render :bar, :layout => false
in the appropriate action.
See also: http://guides.rubyonrails.org/layouts_and_rendering.html
EDIT:
Please see the documentation linked above. For JSON rendering, you might use this directly from the controller:
render :json => #product
or in your case
render :json => [1,2,3,4]
You could also use JSON view templates (for which you'll need to render again). Using a bar.html.erb template for json is not only tricky as you noticed, it will also produce the wrong content type headers.

Related

How to open a spec runner in a browser

In this tutorial it says:
"Opening the spec runner in a web browser will give the expected outcome:"
But does not mention how to do it. How can I do it?
Example code:
var My = {
sqrt: function(x) {
if (x < ) throw new Error("sqrt can't work on negative number");
return Math.exp(Math.log(x)/2);
}
};
Test code:
describe("sqrt", function() {
it("should compute the square root of 4 as 2", function() {
expect(My.sqrt(4)).toEqual(2);
});
});
There should be a SpecRunner.html file that references jasmine, your code, and your spec files. Looks something like this:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jasmine Spec Runner v2.0.2</title>
<link rel="shortcut icon" type="image/png" href="libs/jasmine-2.0.2/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="libs/jasmine-2.0.2/jasmine.css">
<script type="text/javascript" src="libs/jasmine-2.0.2/jasmine.js"></script>
<script type="text/javascript" src="libs/jasmine-2.0.2/jasmine-html.js"></script>
<script type="text/javascript" src="libs/jasmine-2.0.2/boot.js"></script>
<script type="text/javascript" src="libs/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="libs/jasmine-jquery.js"></script>
<!-- include source files here... -->
<script type="text/javascript" src=""></script>
<!-- include spec files here... -->
<script type="text/javascript" src=""></script>
</head>
<body>
</body>
</html>

Rails: How do I load my local Angular 2 app inside a view?

I have an angular 2 app that I built using the angular cli generator: https://github.com/EdmundMai/angular-2-playing
This is the generator: https://github.com/angular/angular-cli
I ran ng serve and now I have a page I can see when I visit http://localhost:4002
My dist/index.html file in the angular 2 app looks like this:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AngularTwo</title>
<base href="/">
<script src="/ember-cli-live-reload.js" type="text/javascript"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="http://carbon.ec2.crowdtap.com/component_importer.js"></script>
</head>
<body>
<angular-two-app>Loading...</angular-two-app>
<script src="vendor/es6-shim/es6-shim.js"></script><script src="vendor/reflect-metadata/Reflect.js"></script><script src="vendor/systemjs/dist/system.src.js"></script><script src="vendor/zone.js/dist/zone.js"></script>
<script>
System.import('system-config.js').then(function () {
System.import('main');
}).catch(console.error.bind(console));
</script>
</body>
<script>
window.carbon.importComponents({ components: ['ct-button', 'ct-button/ct-button-link', 'ct-dialog', 'ct-progress'] });
</script>
</html>
In my rails view, I am trying to include these JS files:
<script src="http://localhost:4200/vendor/es6-shim/es6-shim.js"></script>
<script src="http://localhost:4200/vendor/reflect-metadata/Reflect.js"></script>
<script src="http://localhost:4200/vendor/systemjs/dist/system.src.js"></script>
<script src="http://localhost:4200/vendor/zone.js/dist/zone.js"></script>
<script src="http://localhost:4200/system-config.js"></script>
<script src="http://localhost:4200/main.js"></script>
However, it's not working and complaining that main.ts:1Uncaught ReferenceError: require is not defined
Is there a proper way of doing this?
You need to change the <base href="/"> tag (line 6) in the generated
dist/index.html
to <script>document.write('<base href="' + document.location + '" />');</script>

How to invoke method in a controller in a ruby file or rake script file

If I have a method in a controller,
For example, I want call the show method in a script without opening the webpages.
Is it possible to do it ?
def index
#joseph_memos = JosephMemo.all
#joseph_memo = JosephMemo.new
end
# GET /joseph_memos/1
# GET /joseph_memos/1.json
def show
binding.pry
end
This should work in Rails console, run rails c and type the following to test.
app.get('/joseph_memos/1')
output = app.html_document.root.to_s
But, if you also want to turn into rake task. Create the lib/tasks/actions.rake in your rails app. And add the following lines.
namespace :actions do
desc "test show action"
task :show, [:id] => :environment do |task, args|
app = ActionDispatch::Integration::Session.new(Rails.application)
app.get("/joseph_memos/#{args[:id]}")
puts app.html_document.root.to_s
end
end
Then run, rake actions:show[1] to see the result.
Here is the result that works (that is, bundle exec rake "actions:show[1]" on zsh)
$bundle exec rake "actions:show[1]"
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<link data-turbolinks-track="true" href="/assets/joseph_memos.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/joseph_memos.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="lZqYRkVfhZUhMK8EQIRsdoX2l4IpTPPhHKB44DSaq7s=" name="csrf-token" />
</head>
<body>
<p id="notice"></p>
Edit |
Back
</body>
</html>
More examples: https://github.com/henrik/remit/blob/master/lib/tasks/dev_events.rake
Hope this is useful for you.
i think it's very much possible but implementing it won't be worth it. An alternative solution would be to create a service class that you can call in the show action and you should also be able to use the same class outside of the controller.
def show
MyShowActionHandler.new(params).do_something
end
class MyShowActionHandler
def initialize(params)
#params = params
end
end
Then just call MyShowActionHandler in a script and it should work as long as you load the rails environment.

grails 2.0 including resources the simple way?

I've been battling various resource inclusion issues in my migration from Grails 1.3.7 from Grails 2.0, probably not understanding a few things to begin with.
Firstly, what does
<g:javascript library="application" />
do? (this was in the default main.gsp provided in Grails 1.3.7).
Secondly, for including jquery across my application, can I just do
<r:require module='jquery' />
<r:layoutResources />
in the top of my main sitemesh page that does the
<g:layoutHead />
...
<g:layoutBody />
and "be done with it", using the
<r:layoutResources />
a second time after the
<g:layoutBody />
Thanks
Yes I struggled a little with this at first too.
So firstly the <g:javascript library="application" /> refers to a module defined in a config/*.Resources.groovy file (default is config/ApplicationResources.groovy), inside that you have named modules, eg:
modules = {
application {
resource url: 'js/jquery/jquery-ui-1.8.15.custom.min.js', disposition: 'head'
}
}
Secondly by example a Grails2 main.gsp (cutdown a lot here):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><g:layoutTitle default="Grails"/></title>
<link rel="stylesheet" href="${resource(dir: 'css', file: 'main.css')}" type="text/css">
<link rel="stylesheet" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css">
<link rel="stylesheet" href="${resource(dir: 'css/redmond', file: 'jquery-ui-1.8.15.custom.css')}" type="text/css">
<g:layoutHead/>
<g:javascript library="jquery"/>
<r:require module="application"/>
<r:layoutResources/>
</head>
<body>
<g:layoutBody/>
<r:layoutResources/>
</body>
</html>
Hope that sets you in the right direction

Problem with client event on Telerik MVC Combobox

I have the following markup in an MVC 3 Razor view. As is, the ComboBox renders properly, but doesn't drop down when I click the dropdown arrow. If I remove the jQuery validate script references that are added to the view by the create view template, all works. What could be wrong here?
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
function provinceChanged() {
var cityCombo = $('#Cities').data('tComboBox');
cityCombo.loader.showBusy();
$.get(url, { provinceId: e.value }, function (data) {
cityCombo.dataBind(data);
cityCombo.loader.hideBusy();
cityCombo.enable();
});
}
</script>
...
<div class="editor-field">
#(Html.Telerik().ComboBox()
.Name("Provinces")
.SelectedIndex(1)
.BindTo(new SelectList(Model.ProvinceList, "ProvinceId", "Name"))
.ClientEvents(events => events.OnChange("provinceChanged"))
)
</div>
Here is how reference jQuery, in my master layout:
<head>
<title>#ViewBag.Title</title>
<link href="#Url.Content("~/Styles/Blueprint/screen.css") rel="stylesheet" type="text/css" />
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
#Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css"))
<script src="#Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>
RESOLVED: I have no idea what else changed, but after putting the jQuery Validate references back just for a code sample, my code now works with them as well.
Check this. You are probably using an older version of jquery.validate.js which conflicts with jQuery.fn.delegate.

Resources