我无法在button_to
内完成任务。以下代码有什么问题?我收到错误消息missing required keys: [:callsign]
。
尝试1:
<%= button_to messages_user_path,
callsign: @character.callsign,
params: {
recipient_callsign: notice.character.callsign
},
class: 'btn btn-default btn-xs post_button',
id: 'message_envelope' do %>
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span>
<% end %>
这也会失败并显示相同的错误消息: 尝试2:
<%= button_to messages_user_path(
params: {
callsign: @character.callsign,
recipient_callsign: notice.character.callsign
}
),
class: 'btn btn-default btn-xs post_button',
id: 'message_envelope' do %>
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span>
<% end %>
此版本显示该页面,但它不会隐藏recipient_callsign
,这可以在网址中看到。这首先击败了使用button_to
的对象,因为我不想在网址中使用recipient_callsign
。为什么不隐藏recipient_callsign
?
尝试3:
<%= button_to messages_user_path(
callsign: @character.callsign,
params: {
recipient_callsign: notice.character.callsign
}
),
class: 'btn btn-default btn-xs post_button',
id: 'message_envelope' do %>
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span>
<% end %>
答案 0 :(得分:1)
<%= button_to 'Button text', messages_user_path(callsign: @character.callsign),
class: 'btn btn-default btn-xs post_button',
id: 'message_envelope',
params: {
recipient_callsign: notice.character.callsign
}
do %>
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span>
<% end %>
首先让路径辅助方法分开:
messages_user_path(callsign: @character.callsign)
路径和网址助手采用哈希。除命名密钥之外提供的任何密钥都将添加到查询字符串中。
举个例子:
micropost_path(id: 6)
=> "/microposts/6"
micropost_path(id: 6, foo: 'bar')
=> "/microposts/6?foo=bar"
button_to的签名基本上是:
<%= button_to 'text', action, options = {} %>
您可以使用params
选项向表单添加输入。
{
class: 'btn btn-default btn-xs post_button',
id: 'message_envelope',
params: {
recipient_callsign: notice.character.callsign
}
}