简体   繁体   English

Ruby on Rails:link_to没有适当的ID

[英]Ruby on Rails: link_to not having appropriate ID

So, I got this error 所以,我得到了这个错误

Couldn't find MenuBar with ID=add_page_to_menu

But my code for the link that creates that error is as follows: 但是我创建该错误的链接的代码如下:

<%= link_to "add", 
              :controller => "admin/menu_bars", 
               :action => "add_page_to_menu", 
               :page => page.id,
               :menu => @menu_bar.id %>

The URL that I get the error on is 我收到错误的网址是

http://example.com/admin/menu_bars/add_page_to_menu?menu=1&page=1

it should look something like example.com/admin/menu_bars/add_page_to_menu/1?menu=1&page=1 (I think, I could be wrong, seeing as how its not working =( 它应该看起来像example.com/admin/menu_bars/add_page_to_menu/1?menu=1&page=1 (我认为我可能是错的,因为它看起来不起作用=(

the corresponding action in the controller: 控制器中的相应动作:

  def add_page_to_menu
    @menu_bar = MenuBar.find(params[:menu])
    @page = LinkPage.find(params[:page])

    @menu_bar.link_pages << @page
    if @menu_bar.save
      format.html { render :action => "edit" }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @menu_bar.errors, :status => :unprocessable_entity }
    end
  end

Routes: 路线:

 map.namespace "admin" do |admin|
    admin.root :controller => :site_prefs, :action => :index
    admin.resources :site_prefs
    admin.resources :link_pages
    admin.resources :menu_bars
  end

Your route is going to eval to 您的路线即将到达

http://example.com/admin/menu_bars/:id?menu=1&page=1

so Rails is looking for a MenuBar with an ID of add_page_to_menu . 因此,Rails正在寻找一个ID为add_page_to_menuMenuBar You need to add a member method to the routes for your custom action. 您需要向路由添加成员方法以进行自定义操作。 The routes should look like this: 路线应如下所示:

map.namespace "admin" do |admin|
  admin.root :controller => :site_prefs, :action => :index
  admin.resources :site_prefs
  admin.resources :link_pages
  admin.resources :menu_bars, :member => { :add_page_to_menu => :get }
end

and the link_to should look something like this: 并且link_to应该看起来像这样:

link_to("add", menu_bar_add_page_to_menu_path(@menu_bar, :page => @page.id)

and the url produced should look something like this: 产生的网址应如下所示:

http://example.com/admin/menu_bars/1/add_page_to_menu?page=1

There's still some optimization to be done in that, but I think it will get you beyond this issue at least. 仍然需要进行一些优化,但是我认为至少可以使您超越此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM