我的Rails路由应该与pushState Ember.js路由一起使用? [英] What should my Rails routes look like to work with pushState Ember.js routes?

查看:188
本文介绍了我的Rails路由应该与pushState Ember.js路由一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之



当构建一个Ember.js应用程序来持久化到Rails应用程序时,应该如何处理Rails路由/看法?我会认为我只需要Rails渲染application.html.erb布局,以便Ember.js应用程序初始化并处理路由/视图/模板。



详细信息:



具体来说,如果我访问 localhost:3000 ,在我的Ember.js应用程序有机会初始化,Rails在项目控制器上点击索引操作。它会抱怨丢失的索引模板。我没有index.html.erb视图,因为我的Ember.js应用程序有一个视图/模板。



我应该为Rails应用程序创建空白视图吗?我的Rails控制器操作是否返回某些东西以防止它发生视图?或者我期望构建正常的Rails视图与Ember.js应用程序视图/模板一起?



如果我创建一个空白的项目/ index.html.erb并访问 localhost:3000 ,Rails将渲染它,Ember.js将从此开始初始化和处理路由。但是,如果我直接访问 localhost:3000 / projects / new ,则Rails抱怨在项目控制器中没有新的操作。我不需要在Rails端的项目控制器上没有新操作。我的Ember.js应用程序正在处理该视图/模板。



最终我只是不确定在Rails应用程序中预期使用Ember.js的约会。 / p>

感谢您的帮助和阅读这一点...



编辑:



我省略了我试图使用Ember.js路由器使用 pushState 历史记录。这会让我非hashbang的URL。这是为什么我遇到与Rails竞争路由我的应用程序的问题的一个原因。



Rails应用程序布局

 < html> 
< body>
< section id =design-archive>< / section>
< / body>
< / html>

Ember.js应用程序:

  @DA = Em.Application.create 
名称:'设计存档'
VERSION:'0.1'
rootElement:'#设计档案'
ApplicationController:Em.Controller.extend()
ApplicationView:Em.View.extend
templateName:'application'

DA.initialize(DA .Router)

Rails路线

  DesignArchive :: Application.routes.draw do 
resources:clients,:only => [:new,:create,,index,:show,:destroy]
resources:projects,:only => [:new,:create,,index,:show,:destroy]

root:to => 'project#index'
end

Ember.js路由:

  DA.Router = Em.Router.create 
location:'history'

root:Em.Route.extend
index:Em.Route.extend
route:'/'
redirectsTo:'projects'

#Actions
doProjects:(router) - >
router.transitionTo('projects')
doProjectsNew:(router) - >
router.transitionTo('newProject')

#路由
项目:Em.Route.extend
路由:'/ projects'
索引:Em .Route.extend
路由器:'/'
connectOutlets:(路由器) - >
router.get('applicationController')。connectOutlet('projects',DA.Project.find())
showProject:Em.Route.transitionTo('project')

项目:Em.Route.extend
路由:'/ projects /:project_id'
connectOutlets:(路由器,项目) - >
router.get('applicationController')。connectOutlet('project',project)
projectsIndex:Em.Route.transitionTo('projects')

newProject:Em.Route .extend
route:'/ projects / new'
connectOutlets:(router) - >
router.get('applicationController')。connectOutlet('projectsNew')

Rails Controller:

  class ProjectsController< ApplicationController 
def index
@projects = Project.all

respond_to do | format |
format.html
format.json {render json:@projects}
end
end
end


解决方案

另一个更新:我已经开始使用 DockYard的教程方法,用于处理一个pushState Ember的Rails路由。 js应用程序。这里有一个例子Rails routes.rb

  EmberApp :: Application。 route.draw do 
class FormatTest
attr_accessor:mime_type

def initialize(format)
@mime_type = Mime :: Type.lookup_by_extension(format)
end

def match?(request)
request.format == mime_type
end
end

get'* foo',: to => 'ember#index',:constraints => FormatTest.new(:html)
get'/',:to => 'ember#index',:constraints => FormatTest.new(:html)
end


In short...

When building an Ember.js app to persist to a Rails app, how should I handle Rails routing/views? I would think I just need Rails to render the application.html.erb layout so the Ember.js app initializes and handles the routing/view/templates.

Details:

Specifically if I visit localhost:3000, before my Ember.js app has a chance to initialize, Rails hits the "index" action on the projects controller. It will complain about a missing index template. I have no index.html.erb view as my Ember.js app has a view/template for it.

Should I be creating blank views for the Rails app? Should my Rails controller actions be returning something to prevent it from rendering a view? Or am I expected to build normal Rails views to go alongside the Ember.js app views/templates?

If I create a blank projects/index.html.erb and visit localhost:3000, Rails will render it, Ember.js will initialize and handle routing from then on. However, if I visit localhost:3000/projects/new directly Rails complains about not having a new action in the projects controller. I do not have "new" action on the projects controller on the Rails side as I don't need it. My Ember.js app is handling that view/template.

Ultimately I'm just unsure of what convention is expected to use Ember.js along side a Rails app.

Thank you for the help and reading this far...

Edit:

I left out the detail that I'm attempting to use the Ember.js Router's ability to use pushState history. This would leave me non-hashbang URL's. This is one reason why I'm having issues dealing with Rails competing to route my application.

Rails Application Layout:

<html> 
<body>   
  <section id="design-archive"></section>
</body>
</html>

Ember.js App:

@DA = Em.Application.create
  name: 'Design Archive'
  VERSION: '0.1'
  rootElement: '#design-archive'
  ApplicationController: Em.Controller.extend()
  ApplicationView: Em.View.extend
    templateName: 'application'

DA.initialize(DA.Router)

Rails Routes:

DesignArchive::Application.routes.draw do
  resources :clients, :only => [:new, :create, :index, :show, :destroy]
  resources :projects, :only => [:new, :create, :index, :show, :destroy]

  root :to => 'projects#index'
end

Ember.js Routes:

DA.Router = Em.Router.create
  location: 'history'

  root: Em.Route.extend
    index: Em.Route.extend
      route: '/'
      redirectsTo: 'projects'

    # Actions
    doProjects: (router) ->
      router.transitionTo('projects')
    doProjectsNew: (router) ->
      router.transitionTo('newProject')

    # Routes
    projects: Em.Route.extend
      route: '/projects'
      index: Em.Route.extend
        router: '/'
      connectOutlets: (router) ->
        router.get('applicationController').connectOutlet('projects', DA.Project.find())
      showProject: Em.Route.transitionTo('project')

    project: Em.Route.extend
      route: '/projects/:project_id'
      connectOutlets: (router, project) ->
        router.get('applicationController').connectOutlet('project', project)
      projectsIndex: Em.Route.transitionTo('projects')

    newProject: Em.Route.extend
      route: '/projects/new'
      connectOutlets: (router) ->
        router.get('applicationController').connectOutlet('projectsNew')

Rails Controller:

class ProjectsController < ApplicationController
  def index
    @projects = Project.all

    respond_to do |format|
      format.html
      format.json { render json: @projects }
    end
  end
end

解决方案

Another update: I've since started using DockYard's tutorial method for handling my Rails routes for a pushState Ember.js application. Here's an example Rails routes.rb:

EmberApp::Application.routes.draw do
    class FormatTest
      attr_accessor :mime_type

      def initialize(format)
        @mime_type = Mime::Type.lookup_by_extension(format)
      end

      def matches?(request)
        request.format == mime_type
      end
    end

    get '*foo', :to => 'ember#index', :constraints => FormatTest.new(:html)
    get '/', :to => 'ember#index', :constraints => FormatTest.new(:html)
end

这篇关于我的Rails路由应该与pushState Ember.js路由一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆