ruby on rails - What is causing NoMethodError in my CRUD? -
i student working on first project on own. user supposed able create to-do list. have form partial displayed , trying render list items through partial. after altering routes.rb , items_controller.rb still receive nomethoderror.
here stack trace.
completed 500 internal server error in 23ms (activerecord: 0.2ms) actionview::template::error (undefined local variable or method `items' #<#<class:0x007fa1c3b4e238>:0x007fa1bd6f2258>): 1: <% items.each |i| %> 2: <%= i.name %> 3: <% end %> app/views/items/_item.html.erb:1:in `_app_views_items__item_html_erb___4201771908086516106_70166314068980' app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__1693372631105662933_70166313045680'
items_controller.rb
class itemscontroller < applicationcontroller def index @items = item.all end def show @item = item.find(params[:id]) end def new @item = item.new end def edit @item = item.find(params[:id]) end def create @item = item.new(params.require(:item).permit(:name)) if @item.save flash[:notice] = "the item added list." redirect_to :show else flash[:error] = "there problem creating item." redirect_to :new end end end
routes.rb
rails.application.routes.draw 'welcome/index' 'welcome/about' # resources :welcome, only: [] # collection # 'about' # 'index' # end # end devise_for :users resources :users, only: [:show] resources :items root to: 'welcome#index' end
users/show.html.erb
<h1>welcome <%= current_user.name %></h1> <p>add item</p> <%= render partial: 'items/form', locals: { item: @new_item } %> <%= render partial: 'items/item', locals: { item: @items } %>
items/_item.html.erb
<% items.each |i| %> <%= i.name %> <% end %>
edit: here users_controller.rb
class userscontroller < applicationcontroller before_action :authenticate_user! def show @user = user.find(params[:id]) @new_item = item.new end end
you need use local variable item
, passed partial _item.html.erb
. :
<% item.each |i| %> <%= i.name %> <% end %>
and ...
class userscontroller < applicationcontroller before_action :authenticate_user! def show @user = user.find(params[:id]) @new_item = item.new @items = item.all end end
Comments
Post a Comment