2015-11-27 17 views
1

真希望這實際上是可能的,但我似乎無法訪問我的形式參數與我的自定義操作發送如何訪問在主動管理表單對象參數

我來這裏的目的是爲用戶在填寫自己窗體,點擊一個預覽按鈕,將顯示他們的帖子將看起來像什麼,我已經創建了視圖這很好,只是傳遞參數是一個問題。

這是我目前的形式

# 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"} 
+0

這意味着當你試圖在你的'collection_action:preview'輸出'params'時它是空白的?可以顯示此操作的控制檯日誌? – nayiaw

+0

@nayiaw更新與控制檯輸出 – Richlewis

+0

更有興趣看到'params'而不是'@ post' .. – nayiaw

回答

0

你想創建一個帖子?當您加載此頁面時,這些字段沒有任何值,因此鏈接不會加載任何參數(您可以檢查預覽鏈接上的元素並查看該鏈接沒有任何參數)。

一種方法是在鏈接路由到控制器之前使用JavaScript來捕獲值。

+0

在我的預覽操作中,我在做'@post = Post.new(permited_pa​​rams [:post])'有@post的新實例可用但未保存 – Richlewis

+0

我的意思是當你加載這個表單時,你沒有任何數據,這就是爲什麼你的'@ post'一直爲零的原因。 Rails不知道你的表單裏面輸入了什麼,除非它是一個表單提交按鈕,'link_to'不會捕獲你的表單數據。 – nayiaw

相關問題