When I want to add a custome view to my existing controller
I need to create the custom_page.erb, add the def custom_page in controller, and add routes get .... This is very repetative.
Is there any generate command, that can do this at once? Maybe something like Rails generate CustomActionToMyController action_name?
Nope there is no direct way to do this.
Check this out Create a new action for existing controller
Related
I have some resource and all my resource direct to observer but i want manage request in controller.
I'm already tired of looking for a solution. How to i create custom controller for my resource?
thanks
Find the route want to change from route list using command php atisan route:list and then create new route from web.php or where ever you want to override it.
Ruby on Rails 3.2: I made a table, a model and controller. I want to run the "new" method in my controller.
Off the top of my head I can think of a form to run the method. What other options are there to run controller methods?
In particular I would like to run the "new" method when viewing certain view files. Thank you
Well, you can add the new action to a link, use a form, and call the action using Ajax. Any other alternative other than those will violate MVC.
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.
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
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.