Reputation: 2997
I have a links model which has all the generic scaffold created for it, however, rather than go to the link#new page, I'd like to submit a form from my homepage that populates a new record.
I only have one text field, but im not sure how to construct the form. I read somewhere you have to specify the controller in the form field but this doesn't appear to be working.
<%= form_for(:link, @link) do |f| %>
<% if @link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@link.errors.count, "error") %> prohibited this link from being saved:</h2>
<ul>
<% @link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Upvotes: 0
Views: 63
Reputation: 24815
You don't need to specify anything if you are using default routes.
If the @link is an object that doesn't exist in database, Rails will automatically think this is a form for #new
. So the form action will be /links
, and method is post
, which is the default resource to #create
In your case, you don't need to do anything, just revise the form code to:
<%= form_for(@link) do |f| %>
....
Besides, you need to prepare @link object in home controller, something like
@link = Link.new
Upvotes: 3
Reputation: 12873
All you have to do is add a url
parameter to the form_for helper
<%= form_for :link, url: your_home_path do |f| %>
Upvotes: 0