I’m new to ruby on rails.
In views/events I have “_form.html.erb” which is rendered in “new.html.erb” by this code:
<%= render "form" %>
Now I want to render “_form.html.erb” in “index.html.erb” which is in the same folder(views/events).
But I get the error “missing template”.
I guess I have to add some thing to controller, please help me to render form in other pages of views…
You “usually” don’t render a form in an index action. Most form partials are setup semantically to expect a
@my_resource, but if you’re doing everything the rails way you’re not going to have a instance variable during your index action. There’s a number of ways you can do this but this is probably the quickest.You probably have some collection (let’s pretend you’re using books) in your index action:
You can just set an instance variable somewhere prior to rendering the form:
Another way to do it would be through passing in some locals to a partial. You’d have to change all of your references in
_formto use a local variable instead. Then you can call render like this: