Tony Vincent
Tony Vincent

Reputation: 14292

Rails 4 app links failing to generate correct URL

I have a rails 4 app under development in c9 cloud IDE and I have a view file that displays entries from different models.
My issue is certain links in the file fails when clicked while other links on the same page seems working. I have this code in my app view to generate links

<%= link_to '確認ここへ', "/#{applist.controller_name.strip}/#{applist.wfs_id}/edit" %>

for most of the models it generates correct URL like ide.c9.io/dname/arubaito_boshu/491/edit while certain models fails like https://funin_teate_shikyu/new/518/edit
Any help much appreciated

Upvotes: 0

Views: 39

Answers (1)

siegy22
siegy22

Reputation: 4413

What you're doing is really the anti pattern of rails links. To generate links in rails please use the according ..._path method.

You can see your routes with the following command:

$ bin/rake routes

Which would give you some output like this:

    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit

From there you can take your prefix for the path method.

With the routes above it's quite simple:

<%= link_to "New User", new_user_path %>

Which will generate this:

<a href="/users/new">New User</a>

If you have dynamic routes you can define a route like this.

# config/routes.rb
Rails.application.routes.draw do
  get '/something/:id' => 'something#show', as: :something
end

then in your controller:

# app/controllers/something_controller.rb
class SomethingController < ApplicationController
  def show
    puts params[:id]
  end 
end

Now if you visit /something/this-is-awesome, you can access the passed value (which in this case is: this-is-awesome) by using: params[:id] (see controller code)

Upvotes: 1

Related Questions