Ruby on Rails 4 Routing/Views/Path - ruby-on-rails

I have 2 questions:
I have a controller called homepage. I have a view called samplegraph in my homepage's view directory. I want to get the routing working correctly such that www.homepage.com/samplegraph takes me to the samplegraph page.
As far as I can tell, the route for it in routes.rb should be something like this:
GET 'homepage/samplegraph' => 'homepage#showgraph1'
If I'm understanding rails routing correctly, this statement routes GET requests to homepage/samplegraph to the homepage controller's showgraph1 action. At this point I'm not particularly sure what the showgraph1 action should be in order to render the view page(samplegraph). At the moment the action is simply empty. I don't really know what to put here.
Second question:
Also, while I was researching rails routing, I was looking into resource based routing. For my purposes, I don't need most of the stuff generated by that. One thing I am interested in is that invoking resource based routing automatically generates Paths for you via helpers(I think?).
How would I generate a Path for my route, such that I'd be able to use a link_to method to link various parts of the application together? Any help/comments would be greatly appreciated.

Firstly, if you want to get 'samplegraph' page rendered by hitting 'www.homepage.com/samplegraph', you will need to update your route.
Replace
get 'homepage/samplegraph' => 'homepage#showgraph1'
with
get '/samplegraph' => 'homepage#showgraph1'
Now in showgraph1 action of your homepage controller, you will need to render samplegraph view page at last line of the action.
render 'samplegraph'
As of you second question, just hit rake routes on your terminal from your app directory. It will show all routes with helpers which you can use with link_to. You will need to append _path to those routes while using with link_to

Like #RAJ said first of all you need to change your route to
get '/samplegraph' => 'homepage#showgraph1'
At this point I'm not particularly sure what the showgraph1 action should be in order to render the view page(samplegraph)
Rails doesn't care if your action is empty or not, it'll still render your actions view even if it's empty. Since your action is named showgraph1 so it'll make rails look for showgraph1.html.erb with path views/homepage/showgraph1.html.erb
To change this behavior you need to use render 'samplegraph' in your action
One thing I am interested in is that invoking resource based routing automatically generates Paths for you via helpers(I think?)
Rails generate path and url helpers for each route and it doesn't depend on how your routes are defined but you can customize your helper methods by specifying as: option
get 'homepage/samplegraph' => 'homepage#showgraph1', as: 'showgraph'
This will make your helper methods showgraph_path and showgraph_url

Related

How to write two different routes but calling same controller function in rails 4?

There two different routes but it calling the same controller function.
two possible routes:
1) ios-mobile-developer-india
2) salesforce-consultant-india
we are technology and location dynamically.
routes.rb:
get ':tech-mobile-developer-:loc' => application#tech_location
get ':tech-consultant-:loc' => application#tech_location
If i search ios-consultant-india, it 'll show the output. But, I want to redirect to ios-consultant-india. can anyone tell how to redirect based on technology?
Have you tried like this get '/stories', to: redirect('/articles')
routing redirect
If you want to perform a redirect at route level, you may want to use the router redirect feature.
get '/:tech-mobile-developer-:loc', to: redirect('/%{tech}-consultant-%{loc}')
In this way you don't need to inject the redirect code in the action itself, and the action will only contain the code responsible to render the action content.

How do I forward an action to another controller in Rails?

Rails 4 - I have an action that handles routes like www.site.com/catalog/shoes/sport/ABC123 for product and www.site.com/catalog/shoes/sport for category
The problem is I have to display some seo-paid links (not my choise, I just have to) in format www.site.com/href, and that link must me handled as some product category. That's not the only case - in future, there might be some other "short"-links, that are pointing to other parts of my site
When I used Symfony, I could just write $this->forward('controller', 'action') and get another controller's output while saving short route, but in Rails I see only redirect_to, and it does not preserve short url?
Assuming I have a route get ':href' => 'index#simple', as: :simple, how do I forward execution to another controller (or just call controller's method?), preserving original route? - In "rails way", of course
Thank you
You can just use 'redirect_to controller_action_path' / redirect_to '/controller/action' from your controller action.

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

routes.rb and controller

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

RoR Routes: Why does the app assume I want the show action with every URL?

So, I'm trying to access other actions in my controller, and it assums I want "show" every time, when I really just want... the action I just had in my URL.
this is in my routes:
map.resources :attachments
And when I do
domain/attachments/any_action?params
I get this error:
ActionController::UnknownAction (No action responded to show. Actions: {list of actions}
Using rails 2.3.8
I think I know what you are getting at.
Yes show is assumed for singular uses and index for plural.
This is a convention.
Much of the power / magic in rails comes from "convention over configuration" and this is one of them. Yes it could have been /attachment/show/ or /attachments/index but why not just eliminate the show and index if these are the most common and have them available as defaults and that is what rails does.
Now, as for actions not being available, lets look at that. First of all, right off the bat, please do a rake routes at the command line and see what you get. Also please indicate which actions you are trying. The restful setup "resources" will make 7 actions available:- index, show, new, create, edit, update and destroy, but not "any action"
The rule in Rails is first route matched, first route served.
Because you define a resource, it will consider everything like domain/attachments/whatever to be show action for the whatever stuff.
So two choices here:
define your other routes and declare them before the resource
nest your other routes inside the resource (I don't know how to do that in Rails 2.x)

Resources