Rails4: Adding Pages to a Controller - ruby-on-rails

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.

Related

What is the proper workflow for storing data without multiple instances of database models?

I'm relatively new to web development. I am using ruby on rails.
If I had a website with blog posts, I would have a Post model inside my DB and would CRUD instances of it. What if I had an "about" page (where users can read about the blog) and wanted to edit a block of text on the "about" page? How would I go about updating the text displayed on that page?
It doesn't sound like it would make sense to store the data in a DB model as I would only have one instance of such model (since there is only a single "about" page).
What would be the appropriate workflow for storing and editing this data if I will only have one instance of it?
Static pages do not have any dynamic data - thus can be just plain HTML files or .html.erb in basic rails views.
All you need to do is create something like:
Have a route to yourpage.com/about.html inside your routes.rb file
get "/about", to: "static_pages#readme"
Have a controller to serve this route
class StaticPagesController < ApplicationController
def about; end
end
Notice there's no dynamic data on the about page so we do not have to set any variables inside a controller. (like you would set #posts in your PostsController)
Have a view template with the data you need to be displayed on /about page
app/views/static_pages/about.html.erb
PS. If you'd like to bypass rails all the way you can drop about.html inside public/ folder and check yourpage.com/about or localhost:3000/about.html in dev environment. (Look for 404.html page for reference), but I'd keep with the rails way.

Rails. How to put few controllers on one page

I am working on Todo app now and I have troubles. After sign in, I am on persons profile(first controller), on it I have button for new project(projects controller-2d controller) and after pressing it, appears button for new tasks(task controller-3d controller). How I can put all of this 3 controller's views on one page. Here an example of what I mean(approximately):http://todo.kzotov.ru/
You can put anything you want in the view. You could eager load the projects and tasks and put it all on the profile page. You also don't have to map controllers and views to models, so if the PersonsController or whatever is not what you're looking for, maybe do something more specific like ProfilesController and host all this functionality there.
MVC
You'll be best reading up on the MVC programming pattern -
The bottom line is that if you send a request to your application, it will only hit one controller#action. Your multiple "controllers" should not be something to consider - you should only look at the single controller action you're accessing at that specific time.
To be more specific about this, let me detail how it all works...
OOP
Ruby (on top of which Rails is a framework), is object orientated.
This is not just a fancy phrase - it's a real pattern of programming, which allows you to focus the flow of your application around the data / objects you want to create. The objects in Rails are derived from your Models - collating & organizing the respective data for your controllers
In order to understand how Rails works - you need to appreciate that everything you do is based on objects. Your routes, actions & data all work together to provide the end-user experience we know from Rails. How that happens is down to you.
Specifically, you want to look what what you're accessing
You don't want to load multiple controllers - you want to build several models and show those. This gives you the ability to show the HTML elements / files you want:
Recommendation
I would make sure you can put all your activity on your single view, which will then mean you have to determine your controller's data in order to provide you with the data you need to show:
#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
def index
#your index
end
end
#app/views/profile/index.html.erb
<%= link_to "task", task_path %>
What you'll probably want to do is create a separate route / method to give them the ability to pull back ajax data when the initial button was clicked. I can detail this if you need it, but what I've given you should be ample food for thought

How to add a view in rails

I am quite new to rails but i have searched a lot how to do this but it doesnt seem to work for me. Im trying to create a new view called request for my model called steppy_steps, so i created a new file in the views directory called request.html.rb, added this to my routes, match '/request' => 'pages#request', also tried get "steppy_tests/home", and lastly added (def request, end), to my Steppy_Tests_Controller.rb but when i check the url it gives me an error:Couldn't find SteppyTest with id=home
I cant figure out what to do any help would be great! Thanks in advance.
You should read up on the MVC programming pattern (which is what Rails is based on)
In order to make a view, you need to have the controller and model aspects in place too. I think you're doing this already, but to help you understand more, I'll outline below:
Views : Controller Actions
If you want to show a view from the steppy_steps controller, you need to first have a controller action set up to handle the request. You'd normally use a self-named controller for this (controller name steppy_steps), and have various actions for that
In your routes, you'll then "capture" the request to your steppy_steps controller like this:
#config/routes.rb
resources :steppy_steps
This will create a set of RESTful routes, which you can then translate into your controller, like this:
#app/controllers/steppy_steps_controller.rb
def index
#Index code
end
def show
#Show code
end
This will allow you to create a views directory which can contain the views for /views/steppy_steps/show.html.erb and /views/steppy_steps/index.html.erb
Routes Are Super Important
The error you're getting is caused by you sending /home to your view
The problem here is that if you're using a route which has an in-built id param (the show action routes have this), then Rails will look for the extra params after the URL
To fix this, you'll have to provide more of your code, but I also believe you'd be better understanding the fundamentals of Rails a little more
Adding Routes
You can add routes & views as you wish
If you're looking to add a requests route / view, I'd do this:
#config/routes.rb
resources :steppy_steps do
collection do
get :requests
end
end
This will allow you to create /steppy_steps/requests

Create a new action for existing controller

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

how to access model data from different views in rails application

i am new to RoR and I am a little confused on this topic. Is the scope of variables in the controller only accessible in it's corresponding view?
for instance say I generated a Post scaffold (with blog_text and title). I also generated a controller called static with a home page view/controller resource. Is there any way I can access/print all of the posts from within my home page view? would i have to do something in my static#home function? I cant jsut do Post.all correct?
The controllers are tied to their corresponding views. But you need to use an instance variable (a variable with '#' in the front e.g. #variable) if you want to be able to use the variable in your views. Also, it doesn't matter what model/view/controller you're in when you request data from your db. So in response to your question, yes you can just do something like
#posts = Post.all
in any controller and then access the posts in your views. This is pretty basic stuff, you should study the guide a bit more.

Resources