emacs yasnippets for rails - ruby-on-rails

I'm using emacs 24.3.1 for rails development and currently I end up with enh-ruby-mode when in my rails model files. I would like to use yasnippets for rails commands eg: typing 'bt' + tab expands to belongs_to.
Should my rails templates be put into my enh-ruby-mode snippets directory?

It looks like the repository for the snippets has enh-ruby-mode as a symlink to ruby-mode. Check your yasnippets snippets directory to see if that symlink exists. If not, either update your yasnippets or add the link yourself.
https://github.com/AndreaCrotti/yasnippet-snippets/blob/32bbd36d9a774b9cab6207523ffb5b24179b6505/enh-ruby-mode

Related

Rails (agile web development with rails 4th edition) After creating a Cart, can not find a file named current_cart.rb?

I am trying to follow an example which named "Finding a Cart" and here is my code
rails new n
cd n
cd bin
rails generate scaffold Cart
rails db:migrate
Seems it works very smoothly, in my book, they said that there is a file named current_cart.rb at folder n\app\controllers\concerns and I must store the ID of the cart in the session by indexing it with the :cart_id symbol, but my problem is, I can not find the file name current_cart.rb in that folder. There is only the file named .keep ?
Should I make a file named current_cart.rb and copy paste the content in that book into that file? Thank you very much for reading my problem.
Anh Bui: I noticed a few points which not looking good to me.
Why did you go inside bin(cd bin) and run scaffold that does not require, you can run the same from folder n itself.
You ran the scaffold for Cart then how you can look for file current_cart.rb.
In general, concerns file won't create by default you need to create that file inside concerns folder for Model or controller.
Create concerns as a module concern_name and include that concern in the model or controller where you wants use concern functionality

Files at a custom dir in app/ not reloading in dev environment on Rails 5.1

I have created a new dir and subdirs in my project, just like the example below:
app/
--- services/
------ comments/
--------- create_comments_service.rb
but for every modification I make in 'create_comments_service.rb' I have to restart my server (in dev environment).
Is there anything I can do to do not have to restart my server every time I modify a file inside this dir/subdirs?
I had to add the following line in config/application.rb so Rails could recognize the files in my custom dir:
Dir[Rails.root.join('app/services/**/*.rb')].each{|rb| require rb}
Sorry, I haven't enough points to make a comment, so I'll add an answer.
In Rails 5 all folders inside app are autoloaded. You can check if the needed subfolder is in the list:
bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'
If you see services/comments in the output then maybe you need to stop your rails server, run spring stop an restart the server after it.
If you don't see - try to change the added line in config/application.rb to
config.autoload_paths += Dir[Rails.root.join('app', 'services', '**/')]
And some words about helpers.I do not agree with Martin - helpers are more often used to move out view-related code.
But he is right, it is not a common practice to make a separate service for every controller action. What is in your create_comments_service.rb?
Based on your comments, you are fighting Rails conventions, and that will almost always be punishing.
If you want to keep your controllers neat, and not clutter them with verbose code, you should put that code in your Helper Methods.
Whenever you generate a controller, it will also generate a helper file. For example rails g controller static will create app/helpers/static_helper.rb Put your excess code here, and in your controller include StaticHelper and you will have all those features from your helper avaliable in your controller keeping it nice and clean.

Override controller test scaffold for rails 5

I tried to override the controller test generator for scaffolding in rails 5.
I found the original file, but can't place it in the template directory.
If I read correctly this post Changing scaffold-controller-generator-templates in Rails, the path should be :
lib/templates/rails/test_unit/controller.rb
or
lib/templates/rails/test_unit/functional_test.rb # from the scaffold sources
Finally I tested lots of combinaisons of directories and files names, but none work.
Does anybody know how to do that ?
How about this path? I found it by chance :)
lib/templates/test_unit/scaffold/functional_test.rb
Maybe following directories are also available for minitest templates
./test_unit
./test_unit/controller
./test_unit/helper
./test_unit/integration
./test_unit/mailer
./test_unit/model
./test_unit/plugin
./test_unit/scaffold

Is there a way to add route to Rails by command line?

I am a beginner in ruby on rails. I found it quite inconvenience to add route to route.rb manually every time I add a new action or page to controller or the project. So I want to know if there is a way using command line rather than editing the route.rb file?
Adding routes to the routes.rb file from the terminal can be easily achieved with sed.
Install sed using the following command (Ubuntu):
sudo apt get install sed
Assuming you are in the root directory of your app, here is the command to add the routes:-
sed -i '23iresources :people' config/routes.rb
This is what it does:
File to add text to is config/routes.rb
Line number to insert text to is 23
-i is the insert flag: the text will be inserted
resources :people is what gets added
Now, the route resources :people will be inserted on line 23 in config/routes.rb file in your Rails app.
I don't know about a commandline way to update routes, but you might consider using wildcards instead, that way a single line in your routes file can allow you to access many pages on your site:
http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
If you do rails generate -h
rails generate -h
Please choose a generator below.
Rails:
assets
controller
generator
helper
integration_test
jbuilder
job
mailer
migration
model
resource
scaffold
scaffold_controller
task
As you can see there is no generator for routes by default. This is one of the points where rails runs out of its Magic. Of course you can write your generator Creating and Customizing Rails Generators & Templates.
But I would recommend, be ready to write code when there isn't a quick default way. Once you become a pro you will quite often see rails magic falling short of the solution your are trying to implement.

Custom Views Scaffolding in Rails Engines

I'm trying to get custom scaffolding working from my engine.
I followed some tutorial on customizing Rails 3.2 scaffolding in a normal Rails App and put my customized templates in the engines /lib/templates/erb/scaffold directory but they don't get picked up by the app that includes the engine. Any suggestions?
Update:
I also tried to override the Rails ScaffoldGenerator's source_path and tried some other paths to put my template in, like:
lib/rails/generators/erb/scaffold/templates
zarazan's answer got me most of the way there, but there are a couple of things wrong with it. Here's what worked for me:
class Engine < Rails::Engine
config.generators do |g|
g.templates.unshift File::expand_path('../../templates', __FILE__)
end
end
Note that this goes in the generators section, not app_generators, and that the path is slightly different.
Also, I think the correct path to store your templates is lib/templates/erb/scaffold, optionally replacing erb with whatever language you are using (like haml or slim.) I know this works for slim. The file names are {_form,edit,index,new,show}.html.erb.
In the file that you declare your engine use this command:
class Engine < Rails::Engine
config.app_generators do |g|
g.templates.unshift File::expand_path('../templates', __FILE__)
end
end
It should shift the preference of what template folder Rails uses by default.
Now just put the template files in lib/templates/erb/scaffold/template_name.erb
Where template_name is one of the following: _form.html.erb, edit.html.erb, index.html.erb, new.html.erb, show.html.erb
Once you include the gem you should be able to use the rails generate scaffold command as normal.
Here is an example of an engine that overrides the default scaffolding in rails:
https://github.com/brocktoncg/gemboree
This is where the template directory is located:
https://github.com/brocktoncg/gemboree/tree/master/lib/templates/erb/scaffold
Are you talking about a controller template? Than you are using the wrong directory. Save your template at
lib/templates/rails/scaffold_controller/controller.rb
Have a look at http://xyzpub.com/en/ruby-on-rails/3.2/templates.html for an example.

Resources