0

Is it possible to use a input as the path of a "link_to"?

<% @call.each do |call| %>
    <%= call.title %>
    <%= link_to "Path Text", call_path(call) %>
<% end %>

The above code will go to the call show page. If in the create form I have a input for the path and it is a string. Is it possible to enter "call_path(call)" and then use that input as the link_to path, eg:

<% @call.each do |call| %>
    <%= call.title %>
    <%= link_to "Path Text", call.path %>
<% end %>

I have tried this code and the link outputs as a link with the url of "call_path(call)"

In the call model I have defined path:string when I created the model and I have :path in the permit statement of the params in the controller.

AJDEV
  • 4,790
  • 3
  • 14
  • 22
  • You can't call Ruby (server side) from the client side (browser). What you *can* do is render the href with the results of a Ruby call (all server side), e.g., `Path Text` – Dave Newton Jul 16 '16 at 02:37
  • @DaveNewton so it is not possible to use input saved in a variable as a path? – AJDEV Jul 16 '16 at 02:44
  • It's not clear to me what you're asking. – Dave Newton Jul 16 '16 at 02:45
  • @DaveNewton Sorry, it is hard to explain. Pretty much when I go to create a post one of the form inputs is assigned to :path. I want to use that :path input as the path of the link_to. So instead of doing `<%= link_to "Path Text", call_path(path) %>` the "call_path(path)" will be replaced by the :path input. Again I am sorry, it is hard for me to put it to words. – AJDEV Jul 16 '16 at 02:50
  • Are you saying that you just want to have a link_to URL, but use a string that's stored in the variable, "call" (with the field/method name being "path")? But when you try to do so in the above code (via `<%= link_to "Path Text", call.path %>`, you just get a link to the "call" show page and not the stored path in in "call.path"? Is this post at all helpful? http://stackoverflow.com/questions/16721816/how-do-i-defined-a-variable-link-to-to-an-external-url – Nolan Zandi Jul 16 '16 at 04:34

1 Answers1

0

It's unclear what exactly you're asking but if what you mean to do is:

  1. User clicks a link
  2. Some action is performed on the server using particular input variables

Then what you can do is create a POST route in your routes file for a new action in some controller that takes in whatever input you need as parameters, and performs the action on the server, and responds appropriately.

Or if what you mean is that when the user types in some input into say a text field, and you want to change the link dynamically, you probably want to use JavaScript to do that on the client-side. Something like this (I'm using JQuery):

$("#textfield").on('input', function(e) {
    var text = $(this).val();
    // TODO: sanitize 'text' input
    $("#link").attr('href', text);
});

I should warn you that if this is what you're trying to do, you should validate the input that the user enters because it creates a vector for a script injection attack.

Vidur
  • 1,442
  • 2
  • 17
  • 37