byCoder
byCoder

Reputation: 9184

Rails routing trouble

According to

rake routes

i have such route

types_model GET /types/model(.:format) types#model

I have such method:

def model
  @models = Model.find(:all, :conditions => { :MOD_MFA_ID => params[:model]})
end

And such view:

- @manufacturers.each do |manufacturer|
    %li      
      = link_to manufacturer.MFA_BRAND, :controller => "types", :method => "model", :model => "111"

But how can I using link to send params to method "model"? For example: localhost/types/model/111 ? So that rails server work normal? (now it gives me that i hav'nt such route)

Upvotes: 0

Views: 69

Answers (3)

Nibbler
Nibbler

Reputation: 493

There are several possibilities, the most important is to keep your application in a RESTful clean model (it will save you a lot of time).

First, you should really defines you model properly :

  • Do you consider that one type as different models ?
  • Do you consider that models is under the tye "directory" ?
  • Do you consider that model is a function of a type object ?
  • Do you consider that model is a global function of the type object ?

Model as nested resource of type

In this case, you consider that a type instance has several models instances (like a company can have users). So you should have two restful controllers : one for model and one for type.

You can define such routes like this :

resources :types do
   resources :models
end

Your RESTful methods in the model controller will be called when going to /types/14/model/1 with type_id of 14 and id of 1.

Regarding your question, I think it is the cleanest way.

Model in types namespace

In this case, the model methods will be included in the types namespace but the controller is for handling model objects only.

namespace :types do
  resources :models
end

It will allow you to call /types/models/1 to call the show function of the model controller.

Model method as a method of a type instance

In this case, the model method is a function of a type instance :

resources :types do
   member do
      :model
   end
end

The method function of the type controller will be called when requesting /types/42/model with id = 42

Model method as a global method for types

In this case, the model method is a global method for the global type class

resources :types do
   collection do
      :model
   end
end

In this case, the model method of the types controller will be called when /types/model is requested.

All is explained here.

Upvotes: 0

ddb
ddb

Reputation: 1406

Add this if you need to retain your methods to your routes.

match "/types/model/:model" => "types#model"

Upvotes: 1

ddb
ddb

Reputation: 1406

Consider naming the model method 'show' and the URL will be /types/111. That would make your code RESTful

Upvotes: 3

Related Questions