我需要提交并保存一些数据。我需要表单中的一些帖子 ID:
def message_post_collection_params
params.require(:message_post).permit(
{ post_ids: [] }
)
end
如何通过 button_to 获取 ID?我的代码:
button_to('Submit', approve_collection_message_posts_path, params: { message_post: { post_ids: ['1', '2'] } }, data: { config: 'Are you sure?', remote: true })
但是报错:
undefined method `permit' for #<String:0x007ffbdf9f1540>
在线 params.require(:message_post).permit(.
我该如何解决?
最佳答案
我对 Mike K 的回答投反对票的原因是参数声明似乎不是问题所在:
我认为问题在于您如何分配 params
在button_to
:
button_to 'Submit', approve_collection_message_posts_path, params: { message_post: { post_ids: ['1', '2'] } }, data: { config: 'Are you sure?', remote: true }
通知:<input type="hidden" name="message_post" value="post_ids%5B%5D=1&post_ids%5B%5D=2" />
这就是您收到错误的原因(您的数组中没有嵌套)——您的参数字面上看起来像:"message_post" => "post_ids%5B%5D=1&post_ids%5B%5D=2"
修复
有一种便宜的方法可以修复它:
def message_post_collection_params
params.permit(post_ids:[])
end
= button_to 'Submit', path_helper, params: { post_ids: ["1","2"] }, data: { config: 'Are you sure?', remote: true }
--
如果你想保留你的嵌套,你可以使用 following comment :
Note that the params option currently don't allow nested hashes. Example:
params: {user: {active: false}}
is not allowed. You can bypass this using the uglyparams:{:"user[active]" => true}
params: {:"message_post[post_ids]" => ["1","2"]}
关于ruby-on-rails - 如何使用 button_to 提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33204073/