【问题标题】:Routing error when adding button with class and id from another page从另一个页面添加带有类和 ID 的按钮时出现路由错误
【发布时间】:2014-10-15 10:30:54
【问题描述】:

我正在尝试将“联系我”按钮添加到另一个页面,该页面可以完美运行。该按钮应以弹出窗口的形式打开对话消息框。我在视图中添加:

= link_to t('.contact_me'), new_my_conversations_path(id: @user.id), class: "btnA login #{@show_contact_me ? 'open' : ''}"

在我添加的控制器中:

@show_contact_me = true if session.delete('openmessage') == 'yes'

我收到错误:

No route matches {:action=>"new", :controller=>"my/conversations", :id=>nil}

对话路径如下:

namespace :my do
    resources :conversations, only: [:new, :create, :index, :show, :update, :edit]do
      get 'new/:id', on: :collection, action: :new, as: :new
    end
end

我尝试添加到路线:

resources :experiences, only: [:index, :show] do
   get '/my/conversations/new/:id' => 'conversation#new',as: :my_new_conversations
   post :remove_item, on: :collection   
end

因为它按钮应该在体验控制器和显示方法中。 仍然没有帮助。 我做错了什么?

【问题讨论】:

  • 您是否阅读过有关命名空间、范围和嵌套资源之间差异的 Rails 指南? guides.rubyonrails.org/…
  • 是的,但我还是初学者。有时它让我困惑在哪里使用它们。也许是因为我没有看到很多例子......

标签: ruby-on-rails ruby routes haml


【解决方案1】:

试试这个...

namespace :my do
    resources :conversations, only: [:new, :create, :index, :show, :update, :edit]do
      ##get 'new/:id', on: :collection, action: :new, as: :new
      member { get :new, as: :new }
    end
end

并使用它..

= link_to t('.contact_me'), new_my_conversations_path(@user), class: "btnA login #{@show_contact_me ? 'open' : ''}"

【讨论】:

  • 我得到错误:未定义的方法“new_my_conversations_path”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 2019-01-20
  • 2014-11-28
  • 1970-01-01
相关资源
最近更新 更多