Ruby on rails 如何在windows上的RubyonRails中获取ID

Ruby on rails 如何在windows上的RubyonRails中获取ID,ruby-on-rails,ruby,windows,Ruby On Rails,Ruby,Windows,我目前正在做简单的积垢。我已经完成了添加新项目的部分工作,显示所有项目,现在我想查看特定项目 问题:我怎么做?我的语法正确吗 注意:如果是,我会遇到erro“未定义的局部变量或方法`item\u showItem\u path”# 你是说?项目\附加项目\路径“ 查看 <h1>Welcome to my First CRUD!</h1> <table border = "1" width="100%"> <tr>

我目前正在做简单的积垢。我已经完成了添加新项目的部分工作,显示所有项目,现在我想查看特定项目

问题:我怎么做?我的语法正确吗

注意:如果是,我会遇到erro“未定义的局部变量或方法`item\u showItem\u 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 %>
<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 %>

您需要在
item\u showItem\u路径中传递项目id

改变这个

对此

您可以在路由中看到,它期望
:id

get'/items/showItem/:id'=>“items#showItem'
。一旦传递对象
t
,rails将从该对象获取id

旁注:

您的命名约定不符合ruby约定。通常在ruby中,我们使用下划线命名方法

e、 g showItem将是show\u项目

我还将变量
t
重命名为
item
,这样更容易理解

控制器中已经有show方法,所以不需要为showItem定义新方法和路由。您可以使用show方法


如果您想遵循Rails惯例,还可以将
addItem
重命名为
defnew

您需要在
项目显示项目路径中传递项目id

改变这个

对此

您可以在路由中看到,它期望
:id

get'/items/showItem/:id'=>“items#showItem'
。一旦传递对象
t
,rails将从该对象获取id

旁注:

您的命名约定不符合ruby约定。通常在ruby中,我们使用下划线命名方法

e、 g showItem将是show\u项目

我还将变量
t
重命名为
item
,这样更容易理解

控制器中已经有show方法,所以不需要为showItem定义新方法和路由。您可以使用show方法


如果您想遵循Rails惯例,还可以将
addItem
重命名为
def new

学习Rails的一部分是学习如何利用这些惯例,因为您的设置比需要的要复杂得多

为资源使用创建CRUD路由的步骤

这将创建以下路由:

这意味着我们可以通过以下方式创建指向项目的链接:

<%= link_to item.name, item_path(item) %>
# Or 
<%= link_to item.name, item %>

学习Rails的一部分是学习如何利用约定为您带来好处,因为您的设置比需要的复杂得多,所以您在这一点上失败了

为资源使用创建CRUD路由的步骤

这将创建以下路由:

这意味着我们可以通过以下方式创建指向项目的链接:

<%= link_to item.name, item_path(item) %>
# Or 
<%= link_to item.name, item %>

只有你吗,先生?我的意思是不是ID?@Angel,你也可以做
t.ID
,但是rails很聪明,一旦你通过了对象,就会找到ID。你的
bundle exec rake路由
显示了什么?您能在rake routes中看到item_showItem吗?您是说localhost:3000/info/routes吗?在终端上,您可以键入
bundle exec rake routes
来查看您的所有路由。只有t sir?我的意思是不是ID?@Angel,你也可以做
t.ID
,但是rails很聪明,一旦你通过了对象,就会找到ID。你的
bundle exec rake路由
显示了什么?你能在rake routes中看到item\u showItem吗?你是说localhost:3000/info/routes吗?在终端上你可以键入
bundle exec rake routes
来查看你的所有路由。在你的路由中没有
item\u showItem\u路径
,你为什么要使用它?在你的路由中没有
item\u showItem\u路径
,你为什么要使用它呢?另一方面,Ruby是一种在命名方面有很强的社区约定的语言。如果你打算为钱而写代码,你应该学习它们,否则你将被视为黑客或二流程序员。也要保持简单愚蠢。别把自己搞糊涂了。如果您的资源是Post,那么请致电您的路由、VAR和控制器Post。在编写代码时,就好像在编写代码让别人理解一样。另一方面,Ruby是一种在命名方面具有非常强的社区约定的语言。如果你打算为钱而写代码,你应该学习它们,否则你将被视为黑客或二流程序员。也要保持简单愚蠢。别把自己搞糊涂了。如果您的资源是Post,那么请致电您的路由、VAR和控制器Post。写代码的时候,就好像你在为别人编写代码一样。
Rails.application.routes.draw do
  root to: "items#index"
  resources :items
end
   Prefix Verb   URI Pattern               Controller#Action
    items GET    /items(.:format)          items#index
          POST   /items(.:format)          items#create
 new_item GET    /items/new(.:format)      items#new
edit_item GET    /items/:id/edit(.:format) items#edit
     item GET    /items/:id(.:format)      items#show
          PATCH  /items/:id(.:format)      items#update
          PUT    /items/:id(.:format)      items#update
          DELETE /items/:id(.:format)      items#destroy
<%= link_to item.name, item_path(item) %>
# Or 
<%= link_to item.name, item %>
<%= button_to 'Delete item', item_path(@item), method: :delete %>

# This is explicit just for the sake of the example
# normally you would just use `form_for(@item)`
<%= form_for(@item, path: item_path(@item), method: :patch) do |f| %>
  <h1>Edit item</h1>
  # ...
<% end %>
class ItemsController < ApplicationController
  before_action :set_item, only: [:show,:edit,:destroy] 

  # GET /items/new
  def new
    @item = Item.new
  end

  # POST /items
  def create
    @item = Item.new(post_params)
    if @item.save
      redirect_to @item, success: 'Item created'
    else
      render :new, error: 'Item was not valid'
    end
  end

  # GET /items
  def index
    @items = Item.all.order('created_at DESC')
  end

  # GET /items/:id
  # We don't even need to declare this since 
  # Rails will render the (items/show) view by convention.
  # 
  # def show
  # end

  # GET /items/:id/edit
  # We don't even need to declare this since 
  # Rails will render the (items/edit) view by convention.
  # 
  # def edit
  # end

  # PUT|PATCH /items/:id
  def update
    if @item.update(item_params)
      redirect_to @item, success: 'Item updated.'
    else
      render :edit
    end
  end

  # DELETE /items/:id
  def destroy
    @item.destroy
    redirect_to items_path, success: 'Item deleted.'
  end

  private
  def item_params
    params.require(:item).permit(:name, :description, :price)
  end
  def set_item
    @item = Item.find(params[:id])
  end
end