Sergey
Sergey

Reputation: 377

How to pass a url path to a form_with partial in Rails?

I'm trying to render the form and pass url and method into it

home/index.html.erb

<%= render partial: "rides/form", locals: { url: rides_path, method: :post } %>

rides/_form.html.erb

<%= form_for(action: @ride, local: true, url: url, method: method) do |form| %>

Error:

undefined local variable or method `url' for #<#<Class:0x00007fec9aae15f8>:0x00007fec9ab30e00>
Did you mean?  URI

Upvotes: 3

Views: 2206

Answers (2)

Roman Kiselenko
Roman Kiselenko

Reputation: 44360

You dont need to pass a *_path helper, they are available everywhere in the views.

Upvotes: 1

Kim
Kim

Reputation: 21

Rails is super magic and if you are using an existing instance variable (@ride), you shouldn't have to explicitly pass in the URL. Try

<%= render "rides/form" %> in your home/index.html.erb file

and in your 'rides/_form.html.erb' use:

<%= form_for @ride do |f| %>...

The form_for helper should automatically send data via POST to the #update action in your Rides controller.

You can read more about it here

http://api.rubyonrails.org/v5.0/classes/ActionView/Helpers/FormHelper.html

Upvotes: 2

Related Questions