2
\$\begingroup\$

I have a drop-down that users can choose what template they are about to receive. I'm pretty novice to Rails but I kind of go the logic Front-end = JavaScript and that's what I did, but this use of JavaScript seems like Rails should be able to handles it.

    <div class="form-group">
        <%= form_tag %>
            <%= select_tag :statement_type, options_for_select(Statement.statement_types.keys.to_a), :class => 'id_select', :id => 'secret_id'  %>
    </div>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <%= link_to "New Form", new_statement_template_path(:statement_type => 'SoW'), :id => "secret_submit", method: :post %>
  </div>

The -only- purpose of the form_tag is because I couldn't get the link_to to successfully launch other-wise.

I make the link dynamic by using this function

$('#secret_id').on('change', function(){
    $('#secret_submit').attr('href', '/statements/new/statement_type='+ $('#secret_id').val());
});

I of course now have a pretty ugly URL, and in addition had to create a custom route, so I am wondering about the 'Rails Way' of doing this. Or at the least, making the URL prettier.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Your link_to is set to POST but you are adding the statement_type on the url like if it were a GET request? Sounds like you are mixing things and you can resolve this using just a single form element without JS if I understand you right.

<%= form_tag new_statement_template_path, method: :post %>
  <div class="form-group">
    <%= select_tag :statement_type, options_for_select(Statement.statement_types.keys.to_a) %>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    <%= submit_tag "New Form" %>
  </div>
<% end %>

Now your controller action will have a the selected option at params[:statement_type].

\$\endgroup\$
0
\$\begingroup\$

This looks incomplete, but the first thing is that you shouldn't need the form tag. A link tag cannot be set dynamically but you could set the window location. For example:

<select id='secret_id'>
  <option value="1">First Type</option>
  <option value="2">Second Type</option>
  <option value="3">Third Type</option>" 
</select>

$('#secret_id').on('change', function() { 
  var statement_type = $('#secret_id').val();
  var href =  '/statements/' + statement_type + '/new';
  window.location = href;
});

also, without seeing your code, it shouldn't be necessary to create a new route, your url's should look like /statements/<statement_type>\new or something similar.

I would also suggest you separate the key for your statement types and a user friendly description. Not use the description as your key. You can do this by providing an array of arrays to options_for_select or, probably better use something like options_from_collection_for_select(statement_types, 'first', 'second')

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.