Link_to和ruby在rails上的路由

时间:2014-07-27 22:56:11

标签: ruby-on-rails ruby

我希望将link_to链接到localhost:3000 / questions / 1,但现在它链接到localhost:3000 / questions

链接到代码

<%= link_to "Your Q's", your_questions_path %>

Routes.rb文件

  Rails.application.routes.draw do
  get "/" => "main_app#index"
  get "/location" => "location#location"
  post "/location/index" => "location#index"
  get "/location/index" => "location#index"
  get "/location/directions" => "location#directions"

  root to: 'questions#index'

  get '/logout', to: 'sessions#destroy', via: :delete
  resources :users, only: [:new, :create]
  resources :sessions, only: [:new, :create]
  resources :questions, except: [:new] do
  resources :answers, only: [:create]
  end

  get '/register', to: 'users#new'
  get '/login', to: 'sessions#new'
  get '/logout', to: 'sessions#destroy', via: :delete
  # get '/questions/your_questions', to: 'questions#your_questions' original
  get '/questions/:id', to: 'questions#show'
  get '/questions', to: 'questions#your_questions', as: 'your_questions'
  get '/search', to: 'questions#search'

4 个答案:

答案 0 :(得分:0)

应该是:

<%= link_to "Your Q's", question_path(1) %>

如果id有任何动态,您需要更新您的问题,并提供有关您在控制器/视图中定义的对象的更多详细信息

答案 1 :(得分:0)

您需要为每个操作指定重命名的路由,即:

get '/questions/:id', to: 'questions#show', as: 'your_question'
get '/questions', to: 'questions#your_questions', as: 'your_questions'

这允许使用your_question_path(1)来引用GET'questions / 1'

您也可以使用资源丰富的路由执行此操作:

  resources :questions, as: 'your_questions', only: :show

答案 2 :(得分:0)

在以下命令中:

 get '/questions', to: 'questions#your_questions', as: 'your_questions'

as:部分说:&#34;嘿Rails,我的男人,创建一个名为your_questions_path&#34;的方法。此方法将生成一个字符串,表示一个URL:&#39; / questions&#39;。如果点击该链接,则会将用户带到(到:部分):&#39;问题#your_questions&#39;。很简单吧?

所以它的工作正常。这个动作应该做的只是列出该用户的所有问题。你的路线很好IMO。动作&#39; your_questions&#39;应该调整以显示当前用户的所有问题。

你知道,通过它显示问题的途径是上面的那个:

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

答案 3 :(得分:0)

<强>路线

除了其他答案之外,您确实需要清理routes文件:

  #config/routes.rb

  root to: 'questions#index'
  get '/search', to: 'questions#search'

  resources :location do
     collection do
       match :index, via: [:get, :post]
       get :directions
       get "/", action: "location"
     end
  end

  resources :answers, only: [:create]
  resources :users, only: [:new, :create], path_names: { new: "register" }
  resources :sessions, only: [:new, :create, :destroy], path_names: { destroy: "signout", new: "login",  } do
     collection do
        delete :logout, action: :destroy
     end
  end
  resources :questions, except: [:new] do
     collection do
        get "questions", action: :your_questions
     end
  end

你需要记住Rails&#39;路由结构是resource-based,这意味着您创建的每个路由都需要基于资源(controller#action)。

你的路线太随意了,这是不真实的 - 到目前为止,你能做的最好的事情就是让你的路线以你的控制器/物体为中心,只根据需要增加额外的路线

enter image description here

-

<强>修正

要解决您的问题,您需要致电show的{​​{1}}行动。如上所述,这可以通过链接到QuestionsController路由,或者如果您想使用自定义路由,使用

来完成

我会做以下

确保您的路线构造正确(可能是您的问题的90%),然后确保您能够调用正确的路径:

show

但是,您遇到的问题是只显示<%= link_to "Your Q's", question_path(1) %> - 如果您想要显示&#34;您的问题&#34;,您最好使用以下内容:

question 1