私は現在、単純なCRUDを行っています。どのように私はそれを行うことができます:私はすでに窓上のルビーのIDを取得する方法
..質問を、新しい項目を追加することの一環で行われ、すべての項目を表示し、今私は、特定のアイテムを表示したいですか?私の構文は正しいですか?
注:はい、私はERRO遭遇していた場合は、 "未定義のローカル変数やメソッド` item_showItem_path」#<のための#は:0xaf75068> はもしかしてitems_addItem_path?"
ビュー
<h1>Welcome to my First CRUD!</h1>
<table border = "1" width="100%">
<tr>
<td>ID</td>
<td>Name</td>
<td>Description</td>
<td>Price</td>
<td>Created At</td>
<td>Updated At</td>
<td>Action</td>
</tr>
<% @items.each do |t| %>
<tr>
<td><%= t.id %></td>
<td><%= t.name %></td>
<td><%= t.description %></td>
<td><%= t.price %></td>
<td><%= t.created_at.strftime("%B, %d, %Y") %></td>
<td><%= t.updated_at %></td>
<td><%= link_to 'View ITEM', item_showItem_path%></td>
</tr>
<% end %>
</table>
<%= link_to 'Add ITEM', items_addItem_path %>
をコントローラ
class ItemsController < ApplicationController
before_action :set_item, only: [:show,:edit,:destroy]
def index
@items = Item.all.order('created_at DESC')
end
def addItem
end
def create
@post = Item.new(post_params)
@post.save
redirect_to @items_path
end
def showItem
@post = Item.find(params[:id])
end
def show
end
private
def post_params
params.require(:item).permit(:name, :description, :price)
end
def set_post
@post = Item.find(params[:id])
end
end
私のショーのビュー
<h1 class="name">
<%= @post.name%>
</h1>
<h1 class="description">
<%= @post.description%>
</h1><h1 class="date">
Submitted <%= time_ago_in_words(@post.created_at)%> Ago
</h1>
<%= link_to 'BACK', items_path %>
ルート
Rails.application.routes.draw do
root "items#index"
# URL Controller#method
get '/items/addItem' => 'items#addItem'
get '/items/' => 'items#index'
post '/items/create' => 'items#create'
get '/items/showItem/:id' => 'items#showItem'
end
、なぜあなたはそれを使用しようとしていますか? –