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.
Related
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.
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
I am trying to accomplish Create and Edit on the same view (and same controller) on Rails 3.2 and Ruby 1.9.2. I have a partial and I am using form_for and the standard form helpers.
I submit using Ajax, so my page (form) is not refreshed or redirected. When the user tries to edit the form, naturally, Rails will create an entry instead of updating it. I was thinking of modifying the Controller's create method to detect existing entries, but I am not sure if this is the correct approach. Thanks.
You can pretty easily include the same form in both create and edit pages by storing it in a partial (typically _form.html.erb) and rendering the form in both create and edit.
This is what rails generate scaffold MODEL FIELD:TYPE FIELD1:TYPE1 will give you, as well.
I created an object named settings. So i also provided its route in the routes.rb file i wrote "map.resources :settings". Now as i'm trying to save to the database with that object, it keeps on getting to the localhost:3000/settings url, which i don't have. i', also having this error
NameError in SettingsController#create
uninitialized constant SettingsController
PLEASE HELP! THANKS!
I am not 100% sure, but I believe you need to have a controller to add a route. Check out this diagram: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:mvc
If you use Rails, you have to stick to its rules. Rails implements the MVC pattern, where the controller has the role to provide the linking between a request started in the client web page (view) to creating, reading, updating and deleting (CRUD) objects (== models). The routes.rb define here the mapping from the URL to controller actions, not directly to the resources. See the "Rails Guides for Routing" for more information.
If you want to use your model objects, Rails provides an easy way to start that: scaffolding. By using rails generate scaffold setting <attr_name1>:<type1> ..., you are able to create the following:
A migration for the database that creates the settings table.
Generation of the model object Setting that maps to the created database table.
A controller SettingsController that allows CRUD for your model objects.
View files for the actions generated for the controller.
You can all do that by hand, but it is a good starting point to begin with. And read the basic tutorials and play with the example applications to get a feeling for Rails ...