简体   繁体   中英

Is it necessary to build Rails views when using Ember.js?

I'm updating a Rails app to utilize Ember.js. Those views that existed within the app prior to integrating ember still work fine, but I've also added several new views. These views have all the necessary ember parts (template, controller, etc), as well as all the Rails parts, excluding the view files.

These views work fine if the user accesses them by clicking on an internal link. However, if the user reloads the page or manually enters the URL, then I get this error:

ActionView::MissingTemplate at /contribute
Missing template pages/contribute, application/contribute with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :coffee]}. Searched in:
  * "/home/sites/whistlr/app/views"
  * "/home/.rvm/gems/ruby-2.0.0-p0@whistlr/gems/devise-3.0.0/app/views"

This is clearly happening because I do not have view files. The question is, is this strictly necessary? Is there some way to tell Rails to just load up the Ember views? Ideally, I'd just delete all the old Rails view files once the conversation is complete.

It isn't necessary, but you need to setup the rails routes.rb to have a catch all route that also renders just like your index page which displays the ember app and its html.

namespace :api do
  # resources go here
end

root :to 'home#index'
match "/*path" => 'home#index'

Note: You want to customize this path pattern to your project, else 404s would also be send here.

It's not necessary to create individual views. The trick is to catch the exception in the application controller and then force it render the layout:

class ApplicationController < ActionController::Base
  rescue_from ActionView::MissingTemplate do |exception|
    render "/layouts/application"
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM