partials - First argument in form cannot contain nil or be empty, rails -
attempting use form_for, have been getting error "first argument in form cannot contain nil or empty." error highlited in first line: <%= form_for @item |f| %> #------error highlited here <%= f.label :task %> <%= f.text_field :name, placeholder: "what have do?" %> <% end %>
partial: <%= form_for @item |f| %> <%= f.label :task %> <%= f.text_field :name, placeholder: "what have do?" %> <% end %>
controller: ```
class itemscontroller < applicationcontroller def index @items = item.all end def new @item = item.new end def create @item = item.new @item.name = params[:item][:name] @item.user = current_user if @item.save flash[:notice] = "item added; go accomplish it!" else flash.now[:alert] = "uh oh, didn't save. try again." render :new end end def show @item = item.find(params[:id]) end end
```
and, show view(where call partial): <%= render partial: 'items/form', item: item.new, locals: {items: @item} %>
so far, research has indicated problem in of these files. have seen write, <%= form_for item.new |f| %>, want avoid because have recommended against it. how error fixed? how make sure first argument isn't empty/nil?(i did create item in rails console)
putting both answers below together, should use:
<%= render partial: 'items/form', locals: {item: @item} %>
and call variable as:
<%= form_for item |f| %>
when saying locals: { item: @item }
saying "access instance variable '@item' 'item'"
Comments
Post a Comment