user3029877
user3029877

Reputation: 25

rails form trying to get the posts path

I cant get the syntax of this correct I'm trying to include :url => posts_path in the form_for section

<% form_for :post do |f| %>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %></p>

<p><%= f.submit %></p>

<% end %>

Upvotes: 1

Views: 52

Answers (2)

Dudo
Dudo

Reputation: 4169

There are a few ways to do this. Like @meager, you want to call Post.new. I usually do it in the controller, though.

posts_controller.rb

@post = Post.new

view

 <% form_for @post do |f| %>
   ...
 <% end %>

You can use a custom path if that's where you want to send the data. So if you have a special case, you can make a route for it

routes.rb

get '/special', to: 'posts#special'

then you can have a form that says...

<% form_tag special_path do |f| %>
   ...
<% end %>

and the params will pass as you'd think. Notice the form_tag instead of form_for though.

Upvotes: 0

user229044
user229044

Reputation: 239291

form_for expects an instance of a model, not a symbol:

<% form_for Post.new do |f| %>
...
<% end %>

Upvotes: 2

Related Questions