I have a span tag which is refreshed by javascript. So in my view, this code will display the number :
<span id="actual_nbr"></span> # Show the number via javascript
The javascript looks like :
document.getElementById("actual_nbr").innerHTML = 1;
Now I want to use this value in a link_to to create an object :
<%= link_to "OK", { :action => "create", :nbr => content_tag(:span, 'actual_nbr') }, :confirm => "Are you sure?", :method => :create %>
If I inspect my element I get :
<a confirm="Are you sure?" data-method="create" href="/purchases?nbr=%3Cspan%3Eactual_nbr%3C%2Fspan%3E" rel="nofollow">OK</a>
And, it returns me an error because span tag is considered as text not html tag :
{"_method"=>"create",
"authenticity_token"=>"7CC6I1HrmFZcT2Qrkriafago1ZGhizX4cJL+FaSyRkw=",
"nbr"=>"<span>actual_nbr</span>"}
What is the best way to do what I'm trying to do ?
Thanks
I used Ajax to resolve my problem.
In my javascript :
function submitForm() {
$.ajax({
type:'POST',
url: '/purchases',
data: $.param({ purchase: {id: variable_id, nbr: variable_nbr }})
});
}
In my view :
<button type="button" onclick="submitForm()">Send data</button>
In my controller :
def create
Rails.logger.debug "***************************************"
Rails.logger.debug params
Rails.logger.debug "***************************************"
@purchase = current_user.purchases.create(purchase_params)
if @purchase.save
redirect_to @purchase.paypal_url(user_path(current_user), purchase_params[:id], purchase_params[:nbr])
else
redirect_to(root_url)
end
end
I also used purchases.create instead of purchases.build