خرید بک لینک

Vote count: 0

Attempting to use form_for, I have been getting the error "First argument in form caot contain nil or be empty." The error is highlited in the first line: <%= form_for @item do |f| %> #------error highlited here <%= f.label :Task %> <%= f.text_field :name, placeholder: "What do you have to do?" %> <% end %> partial: <%= form_for @item do |f| %> <%= f.label :Task %> <%= f.text_field :name, placeholder: "What do you have to 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, it didn't save. Try again."
      render :new
    end
  end

  def show
    @item = Item.find(params[:id])
  end
end

```

And, the show view(where I call the partial): <%= render partial: 'items/form', item: Item.new, locals: {items: @item} %>

So far, my research has indicated the problem could be in any of these files. I have already seen that I could simply write, <%= form_for Item.new do |f| %>, but I want to avoid that because all have recommended against it. How is this error fixed? How do I make sure the first argument isn't empty/nil?(I did create an item in the Rails console)

asked 2 mins ago

2 Answers

Vote count: 0

In a partial, your variables are accessible without the @ prefix (i.e. item). They are not instance variables.

If you need to determine whether or not a "local" was passed to a partial, use Ruby's defined?.

answered 44 secs ago

Vote count: 0

Just replace items: @item with item: @item

answered 10 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 3 ارديبهشت 1395 ساعت: 3:19

صفحه بندی