2

This may have been asked before and i can not find anything that would work yet

I am tring to pass a little bit of information into a button that will forward a user to another page.

function addMultiple(){
    var id = $("#truck_company_id").val();
    //dont really want to do this: 
    //window.location.replace("http://www.website.com/companies/"+id+"/trucks");
    //want to do it this way so will work in development
    window.location = <%= trucks_company_path(id) %>;
}

all i need to do is direct a user to the correct website after they click this button.

<button id="class-list" onclick="addMultiple()" class="btn btn-warning pull-right" style="margin: 0;">Add Multiple Trucks</button>

3 Answers 3

2

You can either get rid of Rails using concatenation of strings:

window.location = '/companies/' + id + '/trucks'

or use a Gem like this one

Sign up to request clarification or add additional context in comments.

Comments

0

Output the path as a data-attribute server side and then retrieve it from the script.

<button data-redirect-url="http://www.website.com/companies/xx/trucks"  id="class-list addMultipleTrucks" class="btn btn-warning pull-right">Add Multiple Trucks</button>

$('#multipleTrucks').click(function(){
    window.location = $(this).data('redirect-url');
});

2 Comments

the thing is the ID changes... it is in a drop down list.. also this is not very friendly to development
Render the output of trucks_company_path(id) as data-redirect-url for every id/option in the dropdown list. When you submit the form, redirect to the data-redirect-url of the currently selected option in the dropdown.
0

Big Al Ruby Newbie, you can't use java-script variable into rails.

So you can use 'data' attribute to get the URL then simply pass id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.