khhey khhey - 1 year ago 72
HTML Question

how to add form to application.html.erb page ruby

I'm quite new to ruby and I'm trying to put the form to add a new post to the "application.html.erb" page.

Somehow I can't get it to work by just copying the code from the "app/views/things/_form.html.erb" page, and also I can't seem to get it to work by copying the code from the "app/views/things/new.html.erb" page.

If I do that I get the error:

First argument in form cannot contain nil or be empty

The error points towards:

<%= form_for(@thing) do |f| %>

This is a code that I find in the "app/views/things/_form.html.erb" page.

Can anybody point me in the right direction? To be honest, I don't even know where to start looking for the answer.

Answer Source

I think you should take a look at ruby on rails MVC model first, and also go through a basic tutorial. Just a quick review of you question:

  1. application.html.erb is usually in the layout folder of rails, it's job is to provide a root template/layout for the whole project if not specified. usually you don't want to put any code that is not commonly needed by all pages.

  2. the reason it is giving you the error First argument in form cannot contain nil or be empty is because, the helper method form_for is expecting the controller to provide a @thing to the view file application.html.erb, in your case, it is obvious that @thing is not exist in your controller or your ApplicationController.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download