简体   繁体   中英

Rails pass input in link_to

How can I pass html element input value in link_to path?

%input#answer{type: "text", required: true}
%p
= link_to "Send", game_answer_path(@game),  class: 'btn btn-default', remote: true

I can get my input value as document.getElementById("answer").value , but how can I pass it in game_answer_path(@game, HERE ) ?

How do you get value in the answer field, if you get it from the backend then you can add to the link time when your view is being rendered so you can do something like,

= link_to "Send", game_answer_path(@game, :answer => @answer),  class: 'btn btn-default', remote: true

if that value is being generated by a user action like user selecting or entering an answer then you need to use to dynamically append it to the value of the anchor (href) tag, using javascript you can do something like

var link = document.getElementById("link").value
document.getElementById("link").href = value + '/' +  document.getElementById("answer").value

replace link and answer with respective id from your dom, if this is not what you are after, update your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM