generating one single view without ember - ruby-on-rails

I'm working on a app where the backend is rails and the frontend is completely written in ember.js. As soon as you hit the site the index page is already ember.
I now need to make an api in order to share a part of my site through an iframe, and I can't seem to generate only that page without the whole ember application getting in the way. I always get the part that I'm generating for the api, but underneath starts the index page of the ember application.
I don't want the ember app for that part at all.
I've tried to setup a different namespace in my rails route in order to separate both, but that doesn't work correctly. Funny thing is that I also have railsadmin included and that one does work correct, i.e. I don't see the rest of my ember app after every railsadmin page.
My routes.rb
App::Application.routes.draw do
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
root :to => 'application#index'
namespace :api do
resources :share_part, only: [:show]
end
end

you have to use different layouts.. I think you are only using one layout and within this the complete asset pipeline js code incl. the ember js is loaded? check out your html source in the browser and you will see the loaded js files.
simple use an other layout or an "if single page not include all js" for your single view and do not include the whole js. your rails_admin engine has his own layout and his own js code..that´s the reason why it works there.

Related

How do I navigate to an Ember Route within my Rails application?

I have a very complex interactive page as part of a Rails application I'm working on, and it's really an SPA. I want to leverage Ember to do this so I have much more manageable code. All I really need Rails for is to ferry data back and forth on this page. Another reason for using Ember is so I don't have all of these nasty jQuery one-liner view templates written in JS (I prefer to use CoffeeScript).
I installed and configured Ember-CLI by following the instructions in the README for the ember-cli-rails gem. I have created a separate layout for any sections of my application that I want to use Ember. I have a route for Products set up to include the Ember JS and CSS resources. I can now see the "Welcome to Ember" message on the page for this route in my Rails application.
But I can't do anything else. I've tried to just create an "About" section for this little Ember application, which is called products. The problems I see so far:
The ember-cli-rails gem initializer does not create a views directory. Why is that?
I have yet to locate an Ember-CLI tutorial that doesn't have just a ton of assumptions built in.
I have an about.js.coffeee in my <ember app>/routes directory, one in <ember app>/controllers directory, one in a <ember app>/views directory that I had to create, and an emblem template in <ember app>/templates directory.
Now for a really basic question. How do I bring up this route in the browser? If I type in the route:
http://localhost:3000/products
I get my Ember page, as I should. But if I put:
http://localhost:3000/products/about
Then Rails tries to handle that route, of course. Which is not at all what I want. This is the step that is missing from the tutorials I have read. I would always recommend that a tutorial be put in front of a laymen in order to find the holes prior to publication. I would be happy to volunteer for that job in order to offer something to the community.
How do I navigate to an Ember route within my Rails application?
You don't.
Back in the days there was ember-rails which let you setup ember through the Rails asset pipeline.
You could basically make it so that your root path in rails would render just enough html to kickstart ember. It was nice, and it just worked. But it was a pretty bad idea.
Ember now has its own build process through Ember-CLI so we don't need Sprockets or the asset pipeline just to serve up ember and its dependencies.
Ember also works perfectly fine deployed with just a static html file - you can just pass the request straight from Apache or NGINX (using mod_rewrite for example). SPA's only need just enough HTML to get the SPA framework rolling and then they fetch in data to populate the views.
If you engrained your Ember app into Rails the request would have to go through Rack all and all the middleware just to render what is basically a static html page - that's a lot of unnecessary overhead.
Separate concerns.
The modern approach is to separate the front-end SPA and the backend API. They can even be developed by different teams. Having everything in one big repository was nice and cozy and you could just push to deploy - but separating the concerns makes better apps.
Not only will your front-end app load faster if it is not slugging through Rails. Your Rails API will run much faster if it can ditch all the parts related to serving assets, sessions and views. It becomes a mean green JSON machine instead of a slugging behemoth.
Routing in ember.js
So load up ember server and get the Ember Inspector plugin. Your route should be at locahost:4200/about
The ember development server will make sure every request to localhost:4200 renders the index.html page.
If you try it with javascript disabled you will notice that any path on locahost:4200 like locahost:4200/foo/bar/baz will render the same html.
But usually Ember will then parse the request url and pass it to your route handler.
This is how you declare a route handler in ember.js:
// app/router.js
Router.map(function() {
this.route('about', { path: '/about' });
});
So when you locahost:4200/about.
Ember will render templates/about.hbs. As of Ember 2.0 views are out. Instead you have templates and components (think partials in Rails).
Ember will also load app/routes/about.js if it exists.
http://guides.emberjs.com/v2.1.0/

Using path helper in partials generate wrong path in rails 3

I have my application "http://www.example.com/test".
When I hit the url I get the index page, where I show list of profiles.
To show each profile in the lists, I have used a partial.
Each of the profiles have link to their own show page.
To provide the link addreess I have used Rals path helper like this
profile_path(:id => whatever_id)
The issue is the generated url is like this:-
/profile/whatever_id
It completely skips the app name which is "test". The expected url by me is
/test/profile/whatever_id
Can any one tell what can be the possible cause of this?
It is depends on what you means under "app name". If the app is mounted under /test with Passenger Apache/Nginx module, then Rails app will honor the server-side prefix, and will generate correct URLs automatically, only the development environment will generate urls in the root scope.
If your app is runs standalone or via proxy setup (typical setup if the frontend webserver is an Nginx server), and you want to run it under a prefixed path then you have to use the scope method like this:
Rails.application.routes do
scope(:path => '/test') do
# the rest of your routes go here
end
end
See detailed info in the documentation.

Rails 4 Route "match" changes the CSS/JS base URL on template

I'm making a blog system for my Rails application and I'm stuck with this problem a while and don't know what's going on with my routes.
I installed a template for my app and all the css/js/images files from this template are in "public/" not in the assets folder. It was the only way I found to make the template working.
My blog system have this routes:
When I access "/blog" it serves the index view and it loads all the assets from "assets" and "public". But when I try to access "/blog/" or the matched route "/blog/category_slug/post_slug" rails tries to load the files from "public/" with this URL:
"base_url/blog/category_slug" and that's really weird!
I'm current using Rails 4.0.2. Any thoughts?
Fixed the problem adding a "/" before the path to the assets:
From: "css/bootstrap.css"
To: "/css/bootstrap.css"
I still don't know why this only happens in the blog controller, but that worked.
Thanks.

Restrict access to images in Rails app

I have rails project. In my project I load images on server (rails 3 + paperclip + devise + cancan). I want to limit access to files (for example, the original image can be viewed only by the administrator). How should I do it?
If you are limiting by an attribute in your database then one way would be to serve the image via a controller. It's not the most performant but it is secure.
I haven't tried this out, but if you were serving the image from a URL like
/images/picture_of_mickey_mouse.png
then you could create a route in your app that responds to /images with a filename attribute and serve all those images through a controller.
e.g.
class ImagesController < ApplicationController
before_filter :authenticate_admin!, :only => [:show]
def show
send_file "/images/#{params[:filename]}", :disposition => 'inline'
end
end
You would want to make sure you sanitize the params[:filename] however, otherwise the user would be able to download any file on your server!
The docs on send_file are here: http://apidock.com/rails/ActionController/DataStreaming/send_file
Files are handled with ActionDispatch::Static. IMO the best solution is provide similar middleware (basing on Rails source code) but with some authentication and insert it before ActionDispatch::Static. This would work with Warden-based solutions (like Devise) since they do authentication in middleware; just ensure your middleware is put after Warden and old plain ActionDispatch::Static is run just after them.
EDIT: One more thing worth noting. It's quite common in production that Nginx (I'm not sure about Apache and others) is configured to serve all the static files by itself, without passing those requests to rack stack. You may need to disable this Nginx' feature.

RoR link_to and assets paths

I have a RoR (1.9.3 - 3.2) application running on IIS 7.5 over the HeliconZoo Module.
It is located in a sub directory like http://server/application/
Now if i use link_to with a direct location like <% link_to "/users" %> it is linked back to http://server/users instead of http://server/application/users (while linking to a controller action works fine).
Also (which is the bigger problem here) the application is precompiling all assets into server/application/public/assets but is searching for them in server/public/assets when rendering.
If i set config.assets.prefix the assets are also being rendered to a different location ("/application/..." as addition everytime) which results in a constant desync and assets are never being loaded.
I can workaround this by changing config.assets.prefix to "/application/public/assets" for precompile and changing it back to default after, but this is pretty annoying.
Is there some kind of config to tell the render to add a prefix ("/application") on direct links and assets?
I've never used either IIS or HeliconZoo for Rails deployment, so this might not work, but I'll take a crack at answering anyhow, as it seems to me what you're really trying to do is get Rails to understand you're deploying it to a subfolder, and that isn't too difficult. Simply encapsulate everything in your routes.rb inside a scope, like so:
scope "/context_root" do
resources :controller
resources :another_controller
match 'welcome/', :to => "welcome#index"
root :to => "welcome#index"
end
I copied this answer directly from here, which might provide more useful information to you.
Please try and open IIS Manager, navigate to the “application” folder, open context menu and select “Convert to Application”. Normally no specific tinkering with Ruby code required.

Resources