Mystic Chimp
Mystic Chimp

Reputation: 115

Link to view in Rails

I simply want a link within an 'Index' view to open the corresponding 'project' view.

routes.rb

get '/project', to: 'projects#new'
get '/projects', to: 'projects#index'
get'/project/:id', to: 'projects#show'
post '/projects/new', to: 'projects#create'

#----Resources----
resources :users
resources :projects
resources :hazards

projects_controller.rb

def show
    @project = Project.find(params[:id])

end

index.html.erb

<td class="ellipsis">
 <%= link_to project.name, project_path(:id) %> 
</td>

So all of the other views are working and I can manually type the project/id and see the view. When I try the link however instead of generating the url project/id it generates project.id and points to the projects#create path.

I know the answer to this will be simple, but I'm blowed if I can figure it out. An explanation will be great, so I can understand my mistake.

Upvotes: 0

Views: 76

Answers (2)

Ganesh
Ganesh

Reputation: 2004

Modify routes for show as follows:

get '/projects/:id', to: 'projects#show'

but instead of above I will suggest you to, use resource full routes

ie. resources :projects

Upvotes: 0

jvillian
jvillian

Reputation: 20263

Both the other answers seem good. I would also offer that you should define your routes using standard resources approach:

resources :projects

Which will give you:

     projects GET    /projects(.:format)              projects#index
              POST   /projects(.:format)              projects#create
  new_project GET    /projects/new(.:format)          projects#new
 edit_project GET    /projects/:id/edit(.:format)     projects#edit
      project GET    /projects/:id(.:format)          projects#show
              PATCH  /projects/:id(.:format)          projects#update
              PUT    /projects/:id(.:format)          projects#update
              DELETE /projects/:id(.:format)          projects#destroy

You can prune those using only: or except: as needed/appropriate.

Then you should be able to do:

link_to project.name project

You'll often see this as any one of the following:

link_to project.name project_path(id: project.id)
link_to project.name project_path(project.id)
link_to project.name project_path(project)
link_to project.name project

All of which are equivalent.

Oh, yes, and as Sebastian Palma notes, you're currently passing a symbol for id here:

link_to project.name, project_path(:id)

In which case, rails thinks the symbol :id is an id, which naturally it is not.

BTW, these declarations:

get  '/project',      to: 'projects#new'
get  '/projects',     to: 'projects#index'
get  '/project/:id',  to: 'projects#show'
post '/projects/new', to: 'projects#create'

Include some non conventional items. Specifically, the first, third, and fourth entries.

In general, when using resourceful routes, you want the object name (i.e., project) to be in the plural form, (i.e., projects). If you wanted to handcraft these, I believe they would look more like:

get  '/projects',       to: 'projects#index'
get  '/projects/new',   to: 'projects#new',    as: :new_project
get  '/projects/:id',   to: 'projects#show',   as: :project
post '/projects',       to: 'projects#create'

Which would then mimic a subset of the routes generated by resources :projects:

   projects GET    /projects(.:format)            projects#index
new_project GET    /projects/new(.:format)        projects#new
    project GET    /projects/:id(.:format)        projects#show
            POST   /projects(.:format)            projects#create

You should handcraft your routes or use resources :projects. You should not do both (as it appears you may be doing).

Upvotes: 1

Related Questions