I have created a simple application and edited the index.erb file so that I have a simple view with a text box and a button.
Now when i click on that button i want it to navigate to a new view. I know that we can add models and in that models we have different .erb files. but i want to create a single .erb file or add it to an existing model so that i can change edit the view and call that view as i press the button.
Is it like for every screen we have to create a model??
I dont know how to do the same, i tried searching but no help so far.
It is not that for every screen you've to create a model rather the reverse. Every model for which you need interfaces you'll create views.
Why don't you start with this guide and move ahead understanding the basics.
Other than CRUD interfaces? You can add a view file directly to the view folder of the controller this model is associated with. For example, if the model is post.rb in
app/models
and it has a corresponding controllerposts_controller.rb
inapp/controllers
and it has corresponding views inapp/views/posts
then you can add your view toapp/views/posts
folder with a corresponding method in the controller which will render that view provided there is a route for that in theconfig/routes.rb
file.Say I want to add a
landing_page.html.erb
view to Post. I would add a method inposts_controller.rb
(although, this is not mandatory. But, might be useful for you to check some conditions before rendering the view):Add a view in app/views/posts directory:
Add a route to the config/routes.rb file:
Now, you can access the page at
http://localhost:3000/posts/:id/landing_page
.No you don't have to create models for each view(.erb). In case you want to add new view to existing model, just add new method(def) into the controller(.rb), and new view(.erb) with the same name as of the new method.
let say there exist a model DemoController.rb in app/Demo. you can add new method to it as like
To navigate from the index view to the new_method, you can write
or