link_to填充了text_field中输入的内容?

时间:2016-12-09 02:39:26

标签: javascript jquery ruby-on-rails ruby ajax

如何传递link_to输入text_field的任何内容?

new.html.erb

<%= f.text_field :name, class: 'form-control', id: "challenge-action-home",maxlength: @maximum_length, placeholder: 'Enter Challenge' %>
<%= link_to create_challenge_path(challenge: {name: ???}), data: { modal: true } do %>
  <span class="glyphicon glyphicon-plus"></span>
<% end %>

<script>
  # Maybe use javascript?
</script>

例如,用户可以输入&#34;运行5英里&#34;进入text_field。如何传递link_to

1 个答案:

答案 0 :(得分:0)

知道了!

<%= f.text_field :name, class: "name_field", id: "challenge-name", maxlength: @maximum_length, placeholder: 'Enter Challenge' %>

<%= link_to "Save", create_challenge_path(challenge: {name: @challenge.name}), data: { modal: true }, class: "create_challenge_link" %>

<script>
  $(".create_challenge_link").click(function() {
    $(this).attr("href", function() {
      return this.href + $('.name_field').val();
    });
  });
</script>