是否有可能使局部的onclick没有的link_to?有的、能使、局部、onclick

2023-09-10 17:48:57 作者:人不猥琐往少年

我想知道是否有可能使局部的onclick没有的link_to?

我一直在寻找答案,但大部分的答案指出,使用的link_to blabla_path 远程:真正的,但我不想通过控制器来处理它,而不是仅仅的意见,一边。

类似于

<%=如果button.click?渲染形式%GT; < - (这显然是行不通的,但这样的事情)

感谢

顺便说一句,我用Rails 4.0.1

====

更新

我已经尝试过动态,使用AJAX和jQuery,它的作品,但它的怪异。请参阅我下面的code,我已经放在链接一个循环中,但新论被点击时,它出现在第一篇文章中,当我在第二/第三/第四点击新论所有的谐音中得到了渲染的第一篇文章。到底是怎么回事?当我点击第二个职位新论,它应该出现在第二个职位,而不是第一个。

 <%@ posts.each办|文章| %>

   &其中;%= post.title%GT;

   &其中;%= post.content%GT;

   <%=的link_to新建批注,new_comment_path,ID:new_comment,遥控:真正的%GT;

<%结束%GT;
 

解决方案

如果你真的要动态地获取视图点击一个链接或按钮,你必须通过控制器来开去。按照MVC规则。但是,在你的情况,你可以尝试这样的加载页面呈现的部分,使其隐藏。并通过的jQuery或JavaScript点击一个按钮或链接只是删除其隐藏属性

link to myasus官方下载 link to myasus appv2.0.1.0.2103.29 安卓版 极光下载站

I was wondering if it's possible to render partial onclick without link_to ?

I have been searching for answers but most of the answers state that using link_to and blabla_path with remote: true but I don't want to handle it through controller, instead just the views side.

Something like

<%= if button.click? render "form" %> <--(this obviously won't work but something like this)

Thanks

By the way, I am using Rails 4.0.1

====

Update

I have tried it dynamically, using ajax and jquery, it's works but it's weird. Please see below my code, I've placed the link inside a loop but when the "New Comment" is clicked, it appear on the first post, when I clicked the "New Comment" on the second/third/fourth all of the partials got render on the first post. What is going on? When I click "New Comment" on the second post, it should appear on the second post instead of the first one.

<% @posts.each do |post| %>

   <%= post.title %>

   <%= post.content %>

   <%= link_to 'New Comment', new_comment_path, id: 'new_comment', remote: true %>

<% end %>

解决方案

If you really want to fetch the view dynamically on clicking a link or button you have to go through by controller. AS Per MVC rule. But in your case you can try something like on loading the page render the partial and make it hidden. and on clicking a button or link just remove its hidden property through jQuery or javascript.