anmaree
anmaree

Reputation: 657

route submit button to custom path

I am trying to route a submit button to a specific path (page), but I believe my syntax is not accurate.

This is what I have now:

<%= submit_tag('Next (Step 2 of 3)'), customer_index_path %>

I am getting the error:

/Users/anmareewilliams/RailsApps/GroupOrderingCopy/app/views/products/index.html.erb:18: syntax error, unexpected ',', expecting ')'
...bmit_tag('Next (Step 2 of 3)'), customer_index_path );@outpu...
...  

I tried this as well:

<%= submit_tag'Next (Step 2 of 3)', customer_index_path %> 

and got no errors in the text editor, but got a Rails error that said:

undefined method `stringify_keys' for "/customer/index":String

How can I accomplish routing my submit to a specific path?

Upvotes: 5

Views: 4144

Answers (2)

vee
vee

Reputation: 38645

You don't include path in submit_tag. You need to define the path in your form's action.

<%= form_tag(customer_index_path) do %>
  <%= submit_tag 'Next (Step 2 of 3)' %> 
<% end %>

This should submit the form to customer_index_path.

Update:

To submit a GET request to #customer_index_path, you need to update the form_tag declaration as follows:

<%= form_tag(customer_index_path, method: :get) do %>
  <%= submit_tag 'Next (Step 2 of 3)' %> 
<% end %>

Upvotes: 7

zeantsoi
zeantsoi

Reputation: 26193

The path to your route must be contained within the argument list, so in the first iteration of your code, ensure that both arguments are contained within your parentheses:

<%= submit_tag('Next (Step 2 of 3)', options) %>

Alternatively, you can pass arguments to the function without parentheses. Make sure that there's a space between the submit_tag and the first argument:

<%= submit_tag 'Next (Step 2 of 3)', options %> 

UPDATE:

Regarding the second argument you're passing to submit_tag, the docs say the following:

 submit_tag(value = "Save changes", options = {}) 

The following are valid options:

  • :data - This option can be used to add custom data attributes.
  • :disabled - If true, the user will not be able to use this input.
  • Any other key creates standard HTML options for the tag.

Note that the path is not a valid value. Rather, the path should be passed as the argument to the opening form_tag helper.

Also, I'm assuming that – because you're not using a form_for – you don't have a resourceful route for this controller. Thus, you'll want to create a custom route for customer_index_path:

# config/routes.rb
get '/customers', to: 'customers#index', :as 'customers_index'

Upvotes: 2

Related Questions