真希望這實際上是可能的,但我似乎無法訪問我的形式參數與我的自定義操作發送如何訪問在主動管理表單對象參數
我來這裏的目的是爲用戶在填寫自己窗體,點擊一個預覽按鈕,將顯示他們的帖子將看起來像什麼,我已經創建了視圖這很好,只是傳遞參數是一個問題。
這是我目前的形式
# 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
的一切,它是(硬編碼)方式
所以PARAMS通過傳遞和輸出在我的預覽視圖,但只要我嘗試訪問窗體對象/ PARAMS沒有被傳遞日
# 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似乎接近,但經過空字符串?有沒有人做過這個?
如果任何人有一個替代解決方案會喜歡聽到它。
感謝
更新
當輸出PARAMS到控制檯我得到這個返回
{"action"=>"preview", "controller"=>"my_admin_panel/posts"}
這意味着當你試圖在你的'collection_action:preview'輸出'params'時它是空白的?可以顯示此操作的控制檯日誌? – nayiaw
@nayiaw更新與控制檯輸出 – Richlewis
更有興趣看到'params'而不是'@ post' .. – nayiaw