link_to逻辑解释请

时间:2014-01-03 17:12:30

标签: ruby-on-rails ruby-on-rails-4

如果我这样做:

<%= link_to "treatments", new_client_treatment %>

这不起作用......为什么?

我也尝试过:

<%= link_to "treatments", :controller => "treatments", :action => "show" %>

没有任何结果。

我为客户提供治疗,每个客户都可以享受更多治疗。

class Treatment < ActiveRecord::Base
    belongs_to :client
end

在客户端模型中

class Client < ActiveRecord::Base
    has_many :treatments
end

路线:

resources :clients do
        resources :treatments
    end


Prefix Verb   URI Pattern                                       Controller#Action
    client_treatments GET    /clients/:client_id/treatments(.:format)          treatments#index
                      POST   /clients/:client_id/treatments(.:format)          treatments#create
 new_client_treatment GET    /clients/:client_id/treatments/new(.:format)      treatments#new
edit_client_treatment GET    /clients/:client_id/treatments/:id/edit(.:format) treatments#edit
     client_treatment GET    /clients/:client_id/treatments/:id(.:format)      treatments#show
                      PATCH  /clients/:client_id/treatments/:id(.:format)      treatments#update
                      PUT    /clients/:client_id/treatments/:id(.:format)      treatments#update
                      DELETE /clients/:client_id/treatments/:id(.:format)      treatments#destroy
              clients GET    /clients(.:format)                                clients#index
                      POST   /clients(.:format)                                clients#create
           new_client GET    /clients/new(.:format)                            clients#new
          edit_client GET    /clients/:id/edit(.:format)                       clients#edit
               client GET    /clients/:id(.:format)                            clients#show
                      PATCH  /clients/:id(.:format)                            clients#update
                      PUT    /clients/:id(.:format)                            clients#update
                      DELETE /clients/:id(.:format)                            clients#destroy
                 root GET    /                                                 index#welcome

如果您需要有关该问题的更多信息,请与我们联系。 谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

new_client_treatment是路线的名称。如果要链接到它,则必须在其末尾添加_path,以便Rails知道它是相对路径。如果您想将其设为绝对路径,请将_url添加到其末尾而不是_path

因此,<%= link_to "treatments", new_client_treatment_path %>可以解决您的问题。

答案 1 :(得分:0)

假设客户端对象类似于

@client = Client.first #only for example you have to give your client object here
<%= link_to "treatments", new_client_treatment_path(@client) %>

它正在寻找的路线是

"/clients/:client_id/treatments/new"