Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails在同一页面中呈现表单_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Rails在同一页面中呈现表单

Ruby on rails Rails在同一页面中呈现表单,ruby-on-rails,ruby,Ruby On Rails,Ruby,这是我的设想 我有一个故事控制器,它有一个创建新帖子的表单: stories.show.erb ... <%=link_to("Create new post",new_story_post_path(@post))%> stories.show.erb ... 此新帖子由Posts controller生成 posts\new.erb <%= form_for [@story,@post] do |f|%> posts\new.erb 我想摆脱传统的行为——我希

这是我的设想 我有一个故事控制器,它有一个创建新帖子的表单:

stories.show.erb
...
<%=link_to("Create new post",new_story_post_path(@post))%>
stories.show.erb
...
此新帖子由Posts controller生成

posts\new.erb
<%= form_for [@story,@post] do |f|%>
posts\new.erb
我想摆脱传统的行为——我希望新的帖子表单显示在同一个“故事”页面中,而不是重新加载整个页面并出现在新页面中

在rails中实现这一点的最佳方法是什么

我认为在您的情况下,最好在“故事秀”视图中添加表单并将其隐藏,然后当用户单击链接创建新帖子时,您可以向他们显示您的表单。

def显示
@story=story.find(参数[:id])
@post=@story.posts.build#假设故事有很多帖子
结束
#show.html.erb
...
//表单字段
#一些_file.js
$(文档)。在(“单击”,“发布链接”,函数()上){
$(“#新帖子”).show();
});
#some.css.scss
#新帖子{显示:无;}
你可以写:

<%= form_for [@story,@post], remote: true do |f|%>

在create.js.erb中,您可以编写

$('#post_block').html("<%= render 'your partial with posts' %>")
$('#post_block').html(“”)

我不认为这里需要Ajax,OP希望在不重新加载的情况下显示新的post表单。我认为这是一个很好的解决方案,但javascript应该显示隐藏表单,而不是链接…@Joel_Blum你能解释一下为什么你认为javascript应该显示表单吗?也许我误解了你,表单是隐藏的,你设置了一个显示$的侦听器(这个)哪个是新表单的链接(但是链接已经可见,我们应该显示隐藏的表单),没有?@Joel_Blum ahh my bad没有注意到我在jquery中使用了这个。更新了我的回答。我还必须添加:remote=>true以链接_,使之生效
$('#post_block').html("<%= render 'your partial with posts' %>")