Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ruby on Rails link_to to link to controller action

I'd just started toying around with Ruby on Rails and had come across an issue with linking to another action in a controller from a particular view. I am almost certain it's an issue (or lack of code) in my routes.rb file, but I think I'm misunderstanding exactly how this file works & what I have to do. I've got a solution but pretty sure it's not the "best way" to do it.

I have one controller called home with two actions, index (which is the default) and newbill. Inside index.html.erb I have:

<h1>Home View</h1>
<%= link_to "new", :controller => "home", :action => "newbill" %>

However I was getting a routing error:

No route matches {:controller=>"home", :action=>"newbill"}

Doing rake routes gives me the following:

root  / {:controller=>"home", :action=>"index"}

I then (following some Googling) added this code to routes.rb

match 'home/newbill' => 'home#newbill', :as => :newbill

And then in my index.html.erb I've got this:

<%= link_to "Name", newbill_path %>

And now this works as expected. My questions however are:

  1. Why does this work? What exactly is going on behind the scenes?
  2. Surely this is not the best way to do it? Adding another match 'home/newbill'... for every controller / action I want to link to seems a rubbish way of doing things.

I really like Ruby, but struggling a bit with this aspect of Rails...routing in general is messing up my head a bit I think!

Any help is much appreciated :D

Thanks,

Jack

like image 823
Jack Franklin Avatar asked Nov 18 '11 23:11

Jack Franklin


People also ask

How do you add a link in Ruby on Rails?

Instead of using the anchor ( <a> ) HTML tags for links, Rails provides the link_to helper to add links. Remember, Ruby code in the views must be enclosed in a <% %> or a <%= %> tag.

How do you pass a value from view to controller in Ruby on Rails?

View is already displayed on the user browser, so Sending Data from View to Controller is only that the user inserts Data. To send Data from View to Controller, it's the same as sending Data in a web service, so we can use GET / POST Request to send it.

What decides which controller receives Ruby?

Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.


2 Answers

I guess the first time your code didn't work because your home controller is defined as a resource.

If you define a controller as a resource in routes.rb file it will support only 7 standard methods (according to REST architecture):

index
new
create
show
edit
update
destroy

If you need any more custom routes you should add them manually, say in your case 'newbill', may go as:

resources :home do
    collection do
      get :newbill
    end
end

But as per my understanding, your newbill method should go to bills controllers new, method not in the home controller.

You are right, Rails routes are little bit confusing (at least for me), but once you understand you can do lots of cool stuff.

Read here for the Rails official routes documentation:

http://guides.rubyonrails.org/routing.html.

like image 195
sameera207 Avatar answered Oct 17 '22 08:10

sameera207


You should check out the Rails Routing guide. A read through will help you understand what is going on behind the scenes.

like image 45
Brett Bender Avatar answered Oct 17 '22 08:10

Brett Bender