【发布时间】:2017-03-28 02:11:06
【问题描述】:
我目前正在做简单的 CRUD。我已经完成了添加新项目的一部分,显示所有项目,现在我想查看特定项目..
问题:我该怎么做?我的语法正确吗?
注意:如果是,我遇到错误“未定义的局部变量或方法 `item_showItem_path' for # 你的意思是? 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
【问题讨论】:
-
你的路由中没有
item_showItem_path,你为什么要使用它?
标签: ruby-on-rails ruby windows