Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i insert a variable in to a href value in a link in rails?

i have:

  <a href="/patients/#{@appointment.patient.id}">
  <%=h @appointment.patient.f_name %> <%=h @appointment.patient.l_name%>
  </a>

but it dose not work due to a syntax error, if i click on the href it goes to http://0.0.0.0:3000/patients/#{@appointment.patient.id}

thanks

like image 388
Mo. Avatar asked Jul 29 '10 23:07

Mo.


People also ask

How do you add a variable to a href?

href”, append the variable to it (Here we have used a variable named “XYZ”). Then we need to append the value to the URL. Now our URL is ready with the variable and its value appended to it. In the example below, we will append a variable named 'XYZ' and its value is 55.

How do I pass a PHP variable in HTML href?

<input type="text" name="example"> <? php $ex = $_REQUEST['example']; ?> <a href="http://example.com/number=<;?php $ex ?>"> it only show http://example.com/number= how to fix it so the $ex will be combine with link ? Try to echo the variable value.


1 Answers

You can do:

<a href="/patients/<%= @appointment.patient.id %>">

But it's generally easier to use link_to:

link_to(@appointment.patient.f_name + " " + @appointment.patient.l_name, 
        :controller => 'patients', :action => 'show', :id => @appointment.patient.id)
like image 117
Shadwell Avatar answered Oct 01 '22 02:10

Shadwell