how to access model data from different views in rails application - ruby-on-rails

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.

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.

Ruby on Rails - Controller without Views

Iam new in Ruby on Rails. Normally I work with other web languages and now (of course) I try to compare it with other languages using on web.
But sometimes i have some problem to understand the philosophy and the character of Ruby on Rails. Of course i understand the concept of MVC.
But now Iam not absolutely sure:
Is ist OK to create and use a controller without views? In some cases you need a "class" for some usefull functionality used by other controllers they have views. Or is it a better style to use a module?
I try to find it out by reading a lot of articles and examples, but didnt find detailed information about this content.
When developing Ruby On Rails apps, it's recommended to put most of your business logic in the models, so for the controllers the logic need to be minimal to provide info for the views, so if it doesn't work with a view chance that you need a controller are really low.
Is it OK to create and use a controller without views
Yep it's okay.
The key thing to keep in mind is that Ruby/Rails is object orientated.
This means that every single you do with your controllers/models etc should have the "object" you're manipulating at its core.
With this in mind, it means you can have a controller without corresponding views, as sometimes, you just need to manipulate an object and return a simple response (for example with Ajax).
--
We often re-use views for different actions (does that count as not having a view):
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def search
render :index, layout: false
end
end
The notion of skinny controller, fat model is sound in principle, you have to account for the times when you may need small pieces of functionality that can only be handled by a controller:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def update
#user = User.find params[:id]
#user.update
respond_to do |format|
format.js {render nothing: true}
format.html
end
end
end
A very rudimentary example of Rails is a drive-thru:
view = input interface
controller = accepts order & delivers
model = gets order packaged etc in backend
There are times when the controller may not need a view (for example, if you update your order with specific dietry requirements), and thus the notion that every controller action has to have a view is false.
It's really about making your controller versatile enough to manage your objects correctly.
Shared controller methods can be placed in ApplicationController. Also, in Rails 4 there are concerns (app/controllers/concerns/) where you can put the modules with methods that can be used by multiple controllers.
Controller handles request and renders corresponding view template. So controller without view is absolutely nonsense.
Every time request will execute this controller, it will just end with missing template error, so you will need to create view folder and put empty files with action name inside of it, which is obviously stupid.

Multiple index actions in Rails

I have a Game model and GamesController. Currently my index page shows first 10 records from the database for the purpose of the application. However, i want to make another page where all of the games are being shown.
My question is, what's the Rails way™ of achieving this purpose? Is it possible to have the index action of my GamesController to make requests based on what URL i want to render? (Something like http:localhost:3000/all)
Rails 3.1 - How do I organize multiple index actions for the same model?
was the closest to my issue, but the question tackles problem from rails 3.1 dating back to 2012.
You can really only have one index action per controller, but there are a couple of ways you can achieve this.
First, simply create a new action and have a separate page. Think of an appropriate name for it and create the controller, route, and view. You can keep the amount of code to a minimum by having much of the view code in a partial and use that in both views.
The other way to do this, if you really only want one action, is to pass a parameter to the index controller and query the database based on that parameter. For instance:
link_to 'link text', game_index_path(:g => 'all')
will create a url like: http://domain.com/game/index?g='all' and in the controller you can do this:
def index
which_games = params[:g] # should be all in this case
#games = Game.where(:criteria => which_games)
end
You can use this same method to implement sorting and filtering and all sorts of things.

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

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.

Resources