Create a new action for existing controller - ruby-on-rails

I know this is probably a newbie question, but is it possible to create a new action (method in controller & associated view) from the command line on an existing controller?
For example, I already have a controller named 'Products'. Could I run:
rails g controller products [new_action]
and then rails would insert:
def [new_action]
end
Into my products controller and create a new file called '[new_action].html.erb' in the views/products/ directory? I have already googled this, but no satisfactory answer was returned. Also, I would just go ahead and try it, but I am pretty far into the development of my current app and really do not want to mess anything up.

I'm pretty sure you won't be able to do this in a 100% automated way. The reason is that Rails doesn't know what you've done with your routes or controller, and it would require some logic to know how to update these existing files. Your best bet is to just add the new action manually. Add the new method to your controller, update your routes file, and add the view. It will probably take 1 minute at most. Also, if you're not using version controller (which your question eluded to), then you don't have to worry about it automatically overwriting something.

we can create manually the action in the controller and view but you should also add test statements that because should be good automated process, something like rails generate controller NAME [action action] option m
m = merge

Rails provide posibility of creating custom generators (but this is more advanced subject), which can be tailored for your needs.
More info:
http://guides.rubyonrails.org/generators.html

Related

Creating Methods to an existing Controller via Terminal

I have an existing model that I'd like to add a number of methods to. While I could manually add the methods to the controller as well as its corresponding style/script templates and routes I'd much rather have them done just as I would've when I created the controller in the first place:
rails generate ControllerName hello goodbye
Is there any way to accomplish this?
I think this is a perfectly valid thing to want to do. However the general concensus seems to be that it is too trivial hence why there is no option for it.
A couple of things you could do though:
If you are only adding one or two actions to an otherwise complex controller you could generate another dummy controller with just those two actions. Then copy over the new bits to your existing controller and trash the dummy one. Something like git is great for keeping a track on what the generator changed here.
Alternatively if you want to add a lot of new actions to a trivial controller you could save off the original files, let the generator overwrite the existing controller and then put back the original bits.
Remember you can always use the --pretend option on the generator to see what it would create without actually doing it. This can be useful especially to see what view files it would create for the actions in your case.
You can do
rails generate controller mycontroller hello goodbye
This would generate controller called mycontroller with methods hello and goodbye.

Rails4: Adding Pages to a Controller

I currently have a Shop's Controller:
http://localhost:3000/shops/testing
and i want to add an about and policy page to the existing shop
http://localhost:3000/shops/testing/about
http://localhost:3000/shops/testing/policy
Do i have to generate a seperate model or views or add to the Controller ?
This Question might sound very stupid, but i'm new to rails and cant get over that Problem.
If someone could enlighten me.
Thank you
If your whole page is a shop, then there's no sense in making a single shop controller that contains every action of the shop. Instead, create controllers for the shops "parts". Normally, you would have a separate controller for static pages like about or policy.
Rails will, by default, search for a file that has the same name as the action of the controller under the folder with the same name as the controller and load it, after the code in the controller is executed.
So, if your controller is Shop, and the action is policy, just add a policy.html.erb file under the views/shop folder. Finally, add this to the routes.rb file:
get 'shop/test/about', to: 'shop#about'
Consider the getting started guide, which covers all this.

Parameters for generating controllers in ruby on rails?

When using the command:
rails generate controller home index
When generating a controller do we always have to have a view be generated? Also, can a controller and a view be generated on its own?
No, you do not always have to generate the action. In your case, if you just wanted to generate an empty controller, you can have:
rails generate controller home
Alternatively, controllers and views can be generated manually.
Maybe you're are getting the scaffolding wrong. It should provide you a basic skeleton for beginning and in the later advances on your work, you will start creating it by yourself.
Every time you need to create something custom means you are advancing so scaffolding will stop fitting your needs.

rails the best way to saving page duration and page loading speed

Hi I'm a beginner of rails and I'm not good at English. so if there is some total nonsense please understand..
I'm trying to record loading speed and page duration in every pages.
I made a database "pages" and method "savepage" in my "Page" model.
To save in every page I put "savepage" method in application controller.
Page.rb
def self.savepage
.
.
.
end
application_controller.rb
before_filter :dosave
def dosave
Page.savepage
end
these kind of format..
My question is
1. am I doing correct? using before_filter to do save in very first of loading process?
2. to save after loading all the contents in a page what should I use?
3. to save after user leave this page what should I use?
I saw before_destroy and after_filter, but I can't find what it is... what filter means.... what action means destroy....
thank you in advance!
before_filter is the first thing which loads before giving request to controller.But your need is completely different . Fundamentally filter are used boolean checking.If certain method is true,it will run otherwise it may not. This filter are further extended and we put code into that filters.(And Even sometimes it is consider as best practice) .
Now, before_filter :dosave might be right but is it not true way of knowing page(UI) loading process. I suggest you to use javascript call or use some manually created helper methods and place it into view .erb files.
May be this will interest you
https://github.com/grosser/record_activities
Log user activities in ROR
what action means ?
Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.
Source : http://guides.rubyonrails.org/action_controller_overview.html
I highly suggest you to read above documentation. It is very necessary for you and it covers topic which you asked here.`
And one more thing,
what is action destroy ?
This is simply an action method just like new. Since, rails follow Convention over configuration ( and its developer too) so they put code which do some delete destroy or some destruction. This make thing simple,otherwise more configuration will require which is against rails policy.

creating a simple html page in ruby

Following the ruby tutorial I am trying to create a simple html page.
I will need a controller, i think for some presentation manipoulation but not DB.
I thought that I would create a model, controller and view but then I see that
rails generate model mainMenu freeData1:string freeData2:string
creates the db script.
In order to achieve a simple managed html page that would not require db, what should I create ?
controler only? what is the best practice?
what methods should I put in it for it to be displayed?
thanks.
Yes, you'll just want a controller for the pages which will also create a views folder for your new controller that you can put HTML/ERB/whatever files in.
Since youre not working with the db, you can probably skip the need of a model.

Resources