所以我的部分表单被加载到我的 div id="secondary"
中。 ,在第一页加载时隐藏。
当用户点击一个名为 toggleSidebar
的类的按钮时,然后是 _form.html.erb
显示。
当用户未像这样登录时,我已经覆盖了部分以显示新表单(即使按下 update
):
<%= simple_form_for(Post.new, html: {class: 'form-horizontal' }) do |f| %>
与看起来像这样的常规版本相反,它包含在
if
中关于同一部分的声明:<% if current_user and current_user.has_any_role? :editor, :admin %>
<%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %>
在我看来,真正的问题是,当有人访问
Update
时,这是用户注销时发生的情况: <%= link_to "Update", "#", class: "togglesidebar" %>
这是完美的,它执行 CSS 并完美地显示部分空表单。
但是,当用户登录时,我希望它发送参数
parent_id: @post
随着侧边栏的执行被切换。这就是正常
new_post_path
的样 subview (即非侧边栏新帖 subview ):<% if current_user %>
<%= link_to "Update", new_post_path(parent_id: @post) %>
<% end %>
这就是我的
PostController#New
好像: def new
@post = Post.new(parent_id: params[:parent_id])
end
如何在常规非
new_post_path
中传递参数版本,还是以另一种方式解决这个问题?
最佳答案
您可能可以使用辅助方法。
只需浏览到 'app' 文件夹下的 'helper' 目录并创建一个类似于 [name]_helper.rb 的文件
在此文件中,通过 [name]Helper 创建一个模块,并在此模块中声明您的辅助方法。
rails 自动需要此模块。
一个小例子可能会对你有所帮助。
app/helper目录下link_helper.rb中的代码
module LinkHelper
def populate_link(link1, link2, parameter)
if current_user
public_send(link2, parameter)
else
link1
end
end
end
View 中的代码是
<%= link_to 'update', populate_link('#', 'new_requirement_path',parameter: 33) %>
关于ruby-on-rails - 如何将参数传递给通过 CSS 显示的表单部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27168225/