0

I just can't get things right inside button_to. What is wrong with the following code? I get the error message missing required keys: [:callsign]. Attempt 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 %>

This also fails with the same error message: Attempt 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 %>

This version shows the page, but it doesn't hide the recipient_callsign, which can be seen in the url. This defeats the object of using button_to in the first place because I don't want recipient_callsign in the url. Why doesn't it hide recipient_callsign? Attempt 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 %>

1 Answer 1

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 %>

First lets separate the path helper method:

messages_user_path(callsign: @character.callsign)

Path and url helpers take a hash. Any keys provided except the named keys will be added to the query string.

Take this example:

micropost_path(id: 6)    
=> "/microposts/6"
micropost_path(id: 6, foo: 'bar')
=> "/microposts/6?foo=bar"

The signature for button_to is basically:

<%= button_to 'text', action, options = {} %>

You add inputs to the form with the params option.

{
  class: 'btn btn-default btn-xs post_button',
  id: 'message_envelope',
  params: {
     recipient_callsign: notice.character.callsign
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, thank you. Although it only worked once I removed 'Button text' ( stackoverflow.com/questions/14986559/… )
Oh, yeah, you where using a block :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.