我希望这实际上是可能的,但我似乎不能访问我的表单参数发送与我的自定义操作。
我的目标是让用户填写他们的表单,点击预览按钮,向他们展示他们的帖子会是什么样子,我已经创建了视图,这很好,只是传递参数是一个问题。
这是我目前的形式
# Create Blog Post
form do |f|
inputs 'Blog' do
f.semantic_errors
f.input :title
f.input :category_id, as: :select, collection: Category.all
f.input :comments, as: :text, input_html: { rows: 10, cols: 10 }
f.input :published, as: :boolean
end
inputs 'Submit' do
f.actions do
f.action :submit
f.action :cancel
f.action :reset
li do
link_to 'Preview', preview_my_admin_panel_posts_path(post: { title: "test", comments: 'comments', category_id: '1' }) # Hardcoded for now
end
end
end
end
# Collection Action to handle object
collection_action :preview, method: :get do
@post = Post.new(permitted_params[:post])
end
因此,一切都是这样的(硬编码),参数通过并输出在我的预览视图中,但只要我尝试访问表单对象/参数没有得到通过:
# Console Output
1 - link_to 'Preview', preview_my_admin_panel_posts_path(post: { title: f.object.title, comments: f.object.comments, category_id: f.object.category_id})
#<Post:0x007f8bbe1fc4c0 id: nil, title: "", comments: "", category_id: nil, slug: nil, published: 0, created_at: nil, updated_at: nil>
2 - link_to 'Preview', preview_my_admin_panel_posts_path(post: { title: f.title, comments: f.comments, category_id: f.category_id })
# Console Output
#<Post:0x007f8bbe1fc4c0 id: nil, title: nil, comments: nil, category_id: nil, slug: nil, published: 0, created_at: nil, updated_at: nil>
3 - link_to 'Preview', preview_my_admin_panel_posts_path(@post)
# Console Output
#<Post:0x007f8bbe1fc4c0 id: nil, title: nil, comments: nil, category_id: nil, slug: nil, published: 0, created_at: nil, updated_at: nil>
不知道还能用它去哪里,f.object.param
看起来很接近,但是通过空字符串?
我可以考虑哪些替代解决方案?
更新
当输出参数到控制台时,我得到这个返回
{"action"=>"preview", "controller"=>"my_admin_panel/posts"}
1条答案
按热度按时间tvokkenx1#
您是否正在尝试创建一个帖子?当你加载这个页面时,字段没有任何值,因此链接不会加载任何参数(你可以检查预览链接上的元素,看到链接没有任何参数)。
一种方法是使用JavaScript在控制器中路由链接之前捕获值。