Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Malgorzata Dlugosz
907 PointsProblem with form
Hey I try to create form from scratch .. I can't just make it work.
Controller named : websites_controller Action named : website Model named : website.rb
Now my form ..
<%= simple_form_for @website do |f| %>
f.input :somefield
<% end %>
In my routes I have -> resources :website And -> get '/website', :to => 'websites/website' This is main view, where the form is
I get funny errors like -> undefined model_name for nil Or -> undefined path websites_path
What do I do wrong?
Thanks in advance

Victor Miclovich
9,108 PointsQuick correction on the controller class, its supposed to subclass ApplicationController
, my bad.
class WebsitesController < ApplicationController
# am assuming that you are trying to create a new website
def new
@website = Website.new # set up instance variable
end
def create
@website = Website.create params[:website]
end
## several other methods left
end
1 Answer

Malgorzata Dlugosz
907 Pointssimple_form_for -> @website, does the website instance variable has to be the same name as controllers name or new insance of the controller ->
def website @website = Website.new end
??

Victor Miclovich
9,108 PointsNot really. The instance variable is defined in your controller (websites_controller.rb
). However, Rails developers follow certain conventions when it comes to defining routes in your routes.rb
.
It is not really wise to make your controllers fat with new methods such as website
: for more information, check out: http://guides.rubyonrails.org/layouts_and_rendering.html and http://guides.rubyonrails.org/routing.html (very important links)
If you want your route to pick up the website
method in your controller, then you can do this (as mentioned in the previous post):
In your routes.rb
get '/website', to: 'websites#website'
In your websites_controller.rb
:
class WebsitesController < ApplicationController
def website
@website = Website.new
end
end
However, I still advise that you follow the RESTful way of working.
Victor Miclovich
9,108 PointsVictor Miclovich
9,108 PointsMake sure that in your webites controller (
websites_controller.rb
) file that you have defined the@website
instance; for exampleIn your
routes.rb
, you probably have that as your bare mininumIts wise to keep the method names as RESTful as possible (7 methods: index, new, show, edit, update, and destroy). Following conventions make it easy to debug. I hope the above helps... a nill class or undefined model name signifies not defining the model instance in the fast place to the expecting action/method.