更新时间:2023-02-12 12:21:40
如果在您看来,您尝试了类似 <% controller.publish %>
的东西,那将调用 publish
动作 在视图呈现时,而不是在用户点击按钮时.
If, in your view, you try something like <% controller.publish %>
, that will call the publish
action when the view is rendered, not when a user clicks on the button.
你可以这样做:
创建一个在请求时调用 publish
的路由.例如,假设路由是 /publish
.
Create a route which will invoke publish
when requested. For example, say the route is /publish
.
在你看来,这样写:
onclick
属性的值必须是有效的 JavaScript,它将在按钮被点击时执行.在 JS 中,设置 window.location
会导致浏览器导航到不同的页面.在这种情况下,我们让浏览器导航到 /publish
,这将导致您的 publish
操作被调用.您在 publish
中呈现的任何内容都会出现在浏览器中.
The value of the onclick
attribute must be valid JavaScript, which will be executed when the button is clicked. In JS, setting window.location
causes the browser to navigate to a different page. In this case, we are making the browser navigate to /publish
, which will cause your publish
action to be invoked. Whatever you render in publish
will then appear in the browser.
在上面显示的 publish
代码中,您的 Ruby 语法是错误的,但我假设它只是一个示例,而不是您正在使用的实际代码.
In the code for publish
which you show above, your Ruby syntax is wrong, but I am assuming that it is just a sample, not the actual code which you are using.