I'm bulding an application with lots of simple scaffolds in rails 4.2.5.1 . I customized my erb scaffold templates and placed them in lib/templates/erb/scaffold.
However, when I run rails g scaffold Course , the new templates are just ignored. No warning is given of might be happening. Any insight on how to debug the problem will be deeply appreciated.
Related
I am building my first Rails engine. I called it my_engine, so it generates the files
lib/my_engine.rb
lib/my_engine/engine.rb
lib/my_engine/version.rb
That all have the module named MyEngine. And in the gemspec, the gem name is also set to my_engine.
If I create a model my_model, it goes into app/models/my_engine/my_model.rb, and is generated as
module MyEngine
class MyModel < ActiveRecord::Base
end
end
If I make a class method in here, and put the gem in a Rails project, it all works fine.
def self.hello
"Hello from your Engine's model!"
end
$ bundle exec rails c
[1] (pry) main: 0> MyEngine::MyModel.hello
=> "Hello from your Engine's model!"
However, I do not want the gem name to be my_engine. But if I change the name in the gemspec to something else, like what-i-really-want-to-name-it, everything stops working. Rails cannot see my model, although it can see my namespace. I am indeed changing the gem name in the Rails app and re-bundling, so it's not an issue of that.
$ bundle exec rails c
[1] (pry) main: 0> MyEngine::VERSION
=> "0.0.1" # default version from engine generation
[2] (pry) main: 0> MyEngine::MyModel.hello
NameError: uninitialized constant MyEngine::MyModel
from (pry):2:in `__pry__'
Why is "this" tied directly to the gem name? Is there any kind of workaround for this? I would really like to have the gem name and the module name to be different values.
Using: Rails 4.2.6, Ruby 2.3.0
The answer is that one of Rails' foundational concepts is:
Convention over configuration.
When you decide to override the principle of Convention with Configuration, that's a Rails anti-pattern. Is it possible to to do it and be happy and have a working Rails app? Sure, but it's not code you would want to show as an example of your work on a Rails app.
So, the convention is that the module name matches the gem name. It's just a convention, but since convention is the mutually agreed law in Rails town, you're going anti-pattern when you don't follow it.
Added in response to OP comment
Rails engines are typified by the fact they use an isolated namespace and isolated resources. Gems don't, so in effect, the answer is yes, using a Rails engine does enforce a namespace convention that does not exist for a gem. And that namespacing is used by the middleware to keep the main_app separated from the engine at runtime. Two examples to illustrate:
An extreme example: you can have an app mounted as as engine on itself. Namespacing isolates one from the other so the middleware services act on the right processes which are only differentiated by the namespace.
Another example: 2 engines mounted on a main_app. Now there are essentially 3 apps running. How would you want to implement non-conventional namespace isolation in this case?
So...
It is possible to hammer in a nonconforming namespace in a Rails engine? Probably. I've never tried. But your engine will not be portable. And worse, someone installing it will not be able to mount another engine that is conforming (and sharing the main_app and the middleware stack) because you've forced them into a configuration maze that breaks a conventional Rails engine. This last part is a theory.
I am trying to do some refactoring but am not able to move my partial without getting a missing template error from the calling template. I renamed and moved the partial, then in the calling template, I changed from:
= render 'slot_fields', f: builder
To:
= render 'slots/slot_create_fields', f: builder
But I get the error:
Missing partial sheets/_slot_fields, application/_slot_fields
I have tried restarting the Rails server and cntl-F5 to reload the page but I am stuck. Thanks in advance for your help.
Rails templates are not cached by default.
It could be possible that your templates get precompiled by the Rails Asset Pipeline.
Try to clean all the precompiled assets with the following
rake assets:clobber
Thanks all for the help but I just figured it out. I was referring to the partial somewhere else (a link_to helper).
I am attempting to migrate a simple rails 4 app from server-side .erb (or .haml) to a single-page-app using backbone.js. Since I am new to this, I followed a Railcast tutorial, #323. The tutorial was done using Rails 3.2, but I used my current Rails 4 gemset. All went well until I attempted to use an .eco template to construct a view, as follows:
class Raffler.Views.EntriesIndex extends Backbone.View
template: JST['entries/index']
render: ->
$(#el).html(#template())
this
When I inspect this with the js console (google chrome), I find that 'JST' is undefined. Is there something I need to include to make this work?
for me
gem 'execjs'
in gemfile solved the problem of JST not being defined.
I am working on an existing RoR app which was written a few years ago. It's testers were less than satisfying so the whole /spec directory was removed and I have to write new rspecs. I have seen somewhere that it is possible to generate non-blank rspecs using scaffold method. When I tried doing
rails generate scaffold rspec:model User
I got the lovely result of
invoke active_record
/home/guy/railsProjects/netAdmin/vendor/ruby/2.0.0/gems/railties-4.1.6/lib/rails/generators/base.rb:258:in `const_defined?': wrong constant name Rspec:model (NameError)
from /home/guy/railsProjects/netAdmin/vendor/ruby/2.0.0/gems/railties-4.1.6/lib/rails/generators/base.rb:258:in `block in class_collisions'
how do I work it out? having something that will write some of the specs, based on the existing models and controllers will save me tons of time.
Thanks in advance!
I'm creating a DB web application with grails for my company and found myself in the need to change the default scaffolding templates.
So far so good, everything gets generated with the modified templates (controllers, views, ..).
Now, however, I get a request to create some "composite screens" with functionalities and a layout that differ from the overwritten templates.
So now my question is: is it possible in grails to create one or more templates (next the the default one) and pass this template name as an argument to the generate-* commands?
Thanks in advance!
EDIT: Adding the template name to the generate commands was just an idea, if it's possible to do this a different way, I'll be happy too.
Grails commands are scripts in grails/scripts. If you follow its logic you will see two things.
1) There is only one parameter passed to the script → domain.
2) Class for generating views is DefaultGrailsTemplateGenerator. You can analyse sourcecode and check what this class offers.
Update
Link to DefaultGrailsTemplateGenerator in GitHub.
I am not sure about the generate command parameters, but if you add another .gsp page into scaffolding directory, I believe it will try to run it through generation process.
So for example I used to have a show.gsp page as well as showBasic.gsp page, which showed fewer properties.