hyperlink in ruby on rails - ruby-on-rails

This is something probably trivial but i can't quite find my way round it:
How do i add a hyperlink from one ruby-file.html.erb to another please?

Ruby uses something called routes. You can create named routes for some operations or you can use resource routes (autogenerated CRUD per resource).
For example if you have route for a model called Car, then calling
link_to "Edit my car", edit_car_path(#car)
in your view will generate a link.
remember that your are linking to a controller action in model/view/controller and not to pages

Related

Should About page be named Abouts?

Ruby on Rails naming convention says controllers should be plural, as should associated views. Models are singular.
That makes sense if you have a controller name orders. It is most likely going to have a model named order. However, should the about page be named abouts? That seems odd.
The about page isn't going to have any associated model. Does that make a difference with singular vs plural naming conventions?
For static content pages (eg. 'about us', 'terms and conditions', 'privacy policy', etc.) I typically stray away from typical REST conventions.
I would create a single PagesController and have one action for each page (eg. def about_us; end. Each action would just render the view with the content for that page.
By following the Rails convention of naming, you can standardize a lot of things in your project but there can be exceptions.
You can use about instead of abouts if you want, then name the file as about_controller.rb and inside the file, have the class name as: class AboutController. You have to match the class and the file name, then it will work just fine. Rails won't complaint about this.
But again, it depends on the use case. In this use case, I don't think you really need a controller for about page :-) You should be able to handle that in any other multi-purpose/common controller.
You can use rails pluralize method to generate plurals. Use rails console for this.
"about".pluralize

Ruby on Rails 4 Routing/Views/Path

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

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.

Creating links using link_to for hierarchical model

Okay, so I am new to Rails and am creating a project management system with the framework. For a couple of my models, views, and controllers I used scaffolding and had no problems. For other parts, I coded all of the parts myself.
So as an overview of my project, at the root of it all you can have many projects. Within all of these projects you can create multiple to-do lists. Within each to-do list, you can have multiple tasks. This is what I meant by "hierarchical" in the title.
I just created my lists page, and when I go to the URL directly in my browser (ex: http://localhost:3000/projects/3/lists/20/tasks/1) the task is displayed correctly. However, I do not know how to format my link that is in one of my to-do list views (the tasks are usually shown below the to-do list but now I want them to show on their own view).
Here is the code I currently have:
<%= link_to "#{task.description}", project_list_tasks_url(#list.id,task.id) %>
I know that the link_to "#{task.description}" is correct because I tried using it with a static URL (Google or something), but project_list_tasks_url(#list.id,task.id) is where I'm having trouble.
Can anybody help me out? I can provide as much code as you'd like from my to-do list or task controllers and views.
A couple of tips to help make routing less confusing. It can be a bit unnerving to get used to.
Routing Rule #1
Always check the output of rake routes to be sure how to call your various routing methods. You might think you know how your routes will play out by looking at routes.rb but you won't know until you look at the compiled routes table.
In your case you're expecting a route with the format:
/projects/:project_id/lists/:list_id/tasks/:id
Be sure that's the case. If it is, your call should look like:
project_list_task_path(#project, #list, task)
Note that the arguments here are :project_id, :list_id and :id, so all three are required in this case. Any in brackets in the path specification can be ignored, like :format usually is.
Routing Rule #2
Use the _path methods unless you strictly require the full URL. They're shorter and the output is easier to read and debug. They also don't inadvertently flip the URL in the browser and cause session problems if you don't properly differentiate between www.mysite.com and site.com.
Routing Rule #3
Don't forget there's a huge difference between #project and #project.id when it's supplied to a routing path method.
The router will always call the to_param method if it's available and this can be over-ridden in your model to produce pretty or friendly URLs. id is for your database and your database alone. to_param is for routing but you shouldn't be calling it manually unless you're doing something exceptionally irregular.
You generally should not nest resources more than one level deep, but putting that aside, the link_to format should be:
link_to task.description, project_list_task_path(#project, #list, task)
i.e. project_link_tasks_url should be project_link_task_url, and you have to pass the #project as the first argument (I'm assuming that your project is named #project). I've switched _url to _path so you can just pass the objects themselves as arguments rather than their ids.
See the documentation on creating paths and URLs from objects for details.

Show action in polymorphic associations

I have a polymorphic resource Discussion that can belong to either the Project, Task, or Subtask.
At one point I wish to reroute to show action of discussions controller. To do that, I need discussion id (which I get from params) and I need to know what the parent is (which I get from params also).
So, to route to show action, I'd have to have 3 cases:
project_discusison_path(#project, #discussion)
task_discussion_path(#task, #discussion)
subtask_discussion_path(#subtask, #discussion)
How to write this 3 cases in one path helper? (looking something like below)
parent_discussion_path(#parent, #discussion)
Remember, I can find and have all the variables. Only writing the path is the problem.
If you're generating a link in a--for instance--link_to, you can use this array syntax:
link_to "Show Discussion", [#parent, #discussion]
Under the hood, this achieves the same thing as the polymorphic_path and polymorphic_url methods:
polymorphic_url([#parent, #discussion])

Resources