It is probably understood that I am pretty new to Rails without me even mentioning it :)
So I have a static controller which is working. Here is the route:
match "/pages/:page" => "pages#team"
And I am able to confirm that this route is working because this controller code outputs things to the console:
def team
print("Output some test code to see if system gets here\n")
end
What I am trying to do is have the controller redirect to a view page that I made. My view page is: team.html.rb and it is located at /app/views/pages/team.html.rb
How can I make the controller redirect there?
Thanks!
Make sure you have an action in pages for team
#in PagesController
def team
end
Then rename your file to:
team.html.erb
Just an FYI, you may want to checkout highvoltage from thoughbot for handling static pages:
https://github.com/thoughtbot/high_voltage
Related
I have a major problem with my application. I am new at RoR and got a task from my instructor.
In the task, there's a file like this in view:
views:
account (file)
edit.html.erb
tips (file)
index.html.erb
I need to reach action for account->tips->index.html.erb. I wanted to get all the tips that logged in user added to the application. For that part, I am using this code:
user = User.find(session[:user_id])
#tips = user.tips
When I rendered that page I am getting this output: Processing by Account::TipsController#index as HTML.
But I don't know how to write that index action or where to write.
I have tried to write directly in AccountController as index action, it's not working. And also tried to write in TipsController, it's not working as well.
PS: I have another file called 'tips' and it also has an index action.
PS: I cannot change the file order.
Thanks in advance,
Have a safe day!
You don't need to add tips folder to the account views folder. Keep it separate. Moreover, you need to show the TipsController. If it is separate, then the hierarchy should be as follows.
-> app/views/tips/index.html.erb
I have found the answer and would like to share:
In controller,
there is automatically created account file which contains tips_controller in it.
Inside of the TipsController under Account is like this:
module Account
class TipsController < ApplicationController
def index
#tips = current_user.tips
end
end
end
Somehow I didn't see this controller for hours!
Have a safe and nice day!
So i'm wanting to have a url path in rails that looks like this.
baseurl/event/gigs
Basically the gigs will have its own show file
So im not sure if this is my php kicking in, However, I've got the file structure up like this
event > gigs > index.html.erb
Is this the correct way? and if so will i then have a controller like this?
class GigsController < EventController
def index
#vars will go here unless their inside the eventcontroller already?
end
end
Edit
So i had a blonde moment,
Whats happening now, Is i'm trying to get the gigs page to work but im getting stuck on the show method trying to get gigs and an id.
The url i'm putting in is /event/gigs
The error im getting
Couldn't find Event with 'id'= gigs
Ideas?
You have a resources :events, which by default generates many routes:
One of them is the GET /events/:id route. The URL you're visiting is hitting this route, and it's searching the DB for an Event ActiveRecord object with the :id 'gigs'.
I believe what you're wanting to do is define a static 'gigs' page under the /events/ namespace,
To do so, add this to routes.rb:
resources :events do
collection do
get '/gigs' => "events#gigs"
end
end
The file structure you're looking for, if you want to have a static /events/gigs page, is the following:
Your controller where you will supply variables and logic for the gigs action:
app/controllers/events_controller.rb
Where you will have the following:
def gigs
# Logic goes here.
end
And the view in question:
app/views/events/gig.html.erb
P.S - Like said above in the comments, you should skim over guides.rubyonrails.org/action_controller_overview.html in order to understand basic Rails concepts (folder structure, what controllers are and how routing works..).
You should check the output of rake routes. That will tell you how the URI's will map to controller actions. That's always a good way of debugging this kind of problem.
But from what you have posted and what #GigaBass recommended, I believe you should be pointing you browser at /events/gigs.
Cheers,
Niels
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
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.
Sorry, I thought I understood this, but now I have to re-evaluate my understanding of routes.rb. Hoping you could help.
A browser request goes to the Application Controller and the Controller tells what to show, right? - what erb file, database stuff, whatever...
In my routes.rb file I have:
root :to => 'static_pages#FAQ'
Until lately I thought what was happening was: routes.rb is looking at my static_pages_controller.rb file, looking at the FAQ method, and then seeing what to do. If there's nothing in the FAQ method - as is the case - then Rails does its magic and goes to my FAQ.html.erb in my View, the closest thing.
But even if I change the name of:
def FAQ
end
in my controller, or delete the static_pages_controller.rb altogether, it still goes to my FAQ.html.erb file. So does routes.rb not even look at controllers? Does it go straight to 'View' files? Thanks for any help.
static page are served first so that is why FAQ.html.erb is always served.
Also "A browser request goes to the Application Controller and the Controller tells what to show, right? - what erb file, database stuff, whatever..."
I think of it this way: The request first goes through routing, then to the controller for the resource in question, the controller being inherited from application_controller, will then query the model as needed, calculating variables as needed which it then uses in comiling the View page, which are compilation, gets sent as HTML.
Your ideas are essentially correct; Rails will attempt to render the static_page's controller's FAQ method, which implicitly renders the "FAQ" view, if no view (or other output) is explicitly rendered by the action.
What you're missing is that Rails will fill in the blanks if any one of the controller/action pieces is missing. All you need to do is define the view and Rails will assume that you're simply not bothering to define an empty action.
Starting a method name with a capital letter seems like a very bad idea in ruby, because ruby will treat any identifiers that start with a capital letter as a constant. The very first thing I would do personally is change 'FAQ' to 'faq' in my routes and controller code. If you really want to have 'FAQ' capitalized in the browser url, you can probably accomplish that via something like:
root :to => static_pages#faq, :as => 'FAQ'
To check that your method is getting called, use the logger:
def faq
Rails.logger.warn "faq method did get called after all"
end