如何避免单击链接上的Rails页面?

时间:2021-09-14 20:45:08

I have a link in a Rails view such that when it's clicked on, I want it to call a particular method but NOT leave the page. I can get it to call the method but it always looks for a non-existent template.

我在Rails视图中有一个链接,当它被点击时,我希望它调用一个特定的方法,但不要离开页面。我可以让它调用该方法,但它总是寻找一个不存在的模板。

I was told something like this would be possible with :remote => true, but it seems to completely ignore it. In the view (create.html.erb), it looks something like this:

有人告诉我这样的事情:remote => true,但它似乎完全忽略了它。在视图(create.html.erb)中,它看起来像这样:

<%= link_to 'Do something', foo_bar_index_path(:param1 => "value", :param2 => "value"), :remote => true %>

I have a defined route in routes.rb:

我在routes.rb中有一个已定义的路由:

resources :bar do
   collection do
      get 'foo'
   end
end

In the 'bar' controller, it does call the "foo" method:

在'bar'控制器中,它会调用“foo”方法:

def foo
  # it does the stuff that's here
end

Then it gives me the following error:

然后它给我以下错误:

Missing template bar/foo, application/foo with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/myrailspath/rails-root/app/views" 

How can I change this to still call the method but not leave the page?

如何将其更改为仍然调用方法但不离开页面?

1 个解决方案

#1


1  

Just check you have these two lines in your application.js

只需检查你的application.js中有这两行

//= require jquery
//= require jquery_ujs

#1


1  

Just check you have these two lines in your application.js

只需检查你的application.js中有这两行

//= require jquery
//= require jquery_ujs