【问题标题】:How to get the ID in ruby on rails on windows如何在 Windows 上的 Rails 上获取 Ruby 中的 ID
【发布时间】: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


【解决方案1】:

学习 Rails 的一部分是学习如何利用约定来发挥自己的优势 - 在这方面你失败了,因为你的设置比它需要的复杂。

要为资源创建 CRUD 路由,请使用 resources

Rails.application.routes.draw do
  root to: "items#index"
  resources :items
end

这将创建以下RESTful 路由:

   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 %>

它还允许您通过在路径保持不变的情况下更改请求方法来对项目执行其余的 CRUD 操作:

<%= 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 %>

请注意,创建也没有特殊路径。您可以通过向收集路径 (/items) 发送 POST 请求来创建新记录。

这是经典网络中最简单的传统 CRUD 控制器的样子:

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

【讨论】:

  • 附带说明 - Ruby 是一种在命名方面具有非常强大的社区约定的语言。如果您打算为金钱编写代码,则应该学习它们,否则您将被视为黑客或二流程序员。 github.com/bbatsov/ruby-style-guide
  • 也保持简单愚蠢。不要混淆自己。如果您的资源是 Post,则调用您的路由、变量和控制器帖子。写得好像你在哪里写代码让别人理解。
【解决方案2】:

您需要在 item_showItem_path 中传递商品 ID

改变这个

&lt;td&gt;&lt;%= link_to 'View ITEM', item_showItem_path%&gt;&lt;/td&gt;

到这里

&lt;td&gt;&lt;%= link_to 'View ITEM', item_showItem_path(t) %&gt;&lt;/td&gt;

&lt;td&gt;&lt;%= link_to 'View ITEM', item_showItem_path(t.id) %&gt;&lt;/td&gt;

你可以在你的路线中看到,它期待:id

get '/items/showItem/:id' =&gt; 'items#showItem'。一旦你传递了对象t,rails 就会从该对象中获取 id。

旁注:

您的命名约定未遵循 ruby​​ 约定。通常在 ruby​​ 中,我们使用下划线命名方法。

例如 showItem 将是 show_item

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

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

如果您想遵循 Rails 约定,也可以将 addItem 重命名为 def new

【讨论】:

  • 只有先生?我的意思不是身份证?
  • @Angel ,您也可以使用 t.id,但是一旦您传递了对象,rails 会很聪明地找到 id。
  • 您的bundle exec rake routes 显示了什么?你能在 rake 路线中看到 item_showItem 吗?
  • 你的意思是 localhost:3000/info/routes 吗?
  • 在终端上你可以输入bundle exec rake routes来查看你所有的路线。
猜你喜欢
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 2011-04-29
  • 1970-01-01
  • 2011-12-24
  • 2021-12-05
  • 2011-09-22
相关资源
最近更新 更多