2017-03-28 6 views
0

私は現在、単純なCRUDを行っています。どのように私はそれを行うことができます:私はすでに窓上のルビーのIDを取得する方法

My routes

..質問を、新しい項目を追加することの一環で行われ、すべての項目を表示し、今私は、特定のアイテムを表示したいですか?私の構文は正しいですか?

注:はい、私は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 
+0

、なぜあなたはそれを使用しようとしていますか? –

答えて

0

あなたitem_showItem_path

変更でこの

<td><%= link_to 'View ITEM', item_showItem_path%></td>

012アイテムIDを渡す必要がありますあなたのルートで見ることができるこの

<td><%= link_to 'View ITEM', item_showItem_path(t) %></td>

または

<td><%= link_to 'View ITEM', item_showItem_path(t.id) %></td>

から

は、それが:id

get '/items/showItem/:id' => 'items#showItem'を期待しています。オブジェクトtを渡すと、railsはそのオブジェクトからIDを取得します。

SIDE注:

あなたの命名規則は、ルビー規則に従っていません。通常はルビではアンダースコアを使用してメソッドの名前を付けます。

例えばshowItemは、私もそれを理解することは簡単ですitemに変数tの名前を変更します

show_itemになります。

すでにshowメソッドがコントローラにあるので、showItemの新しいメソッドとルートを定義する必要はありません。あなたはshowメソッドを使うことができます。

また、Rails規約に従う場合は、addItemdef newと名前を変更することもできます。

+0

?私はIDじゃないの? – Angel

+0

@Angel、あなたは 't.id'でも行うことができますが、一度あなたがオブジェクトを渡すと、レールはスマートでIDを見つけることができます。 – Reboot

+0

'bundle exec rake routes'には何が表示されますか?あなたはレーキルートでitem_showItemを見ることができますか? – Reboot

2

Railsを学ぶことの一部は、設定がより複雑になるため、あなたが失敗している、あなたの利点に慣習を使用する方法を学んでいます。

は、リソースの使用 resources

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

これは、次のRESTfulルートを作成するためのCRUDルートを作成するには:

:我々はによって項目へのリンクを作成することができることを意味し

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

作成には特別なパスがないことに注意してください。 POST要求をコレクションパス(/items)に送信して、新しいレコードを作成します。

これは古典的なウェブのための最も単純な従来のCRUDコントローラは次のようになります。

は何の `item_showItem_path`はルートではありません
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 
+1

サイドノート - Rubyは名前付けに関して非常に強力なコミュニティ規約を持つ言語です。あなたがお金のためのコードを書くつもりなら、あなたはそれらを学ぶべきです。そうすれば、あなたはハックまたはセカンド・レートのプログラマーとみなされます。 https://github.com/bbsosov/ruby-style-guide – max

+0

Keep It Simple Stupid。自分を混乱させないでください。あなたのリソースが投稿の場合は、あなたのルート、varsとコントローラのポストを呼び出します。あたかも他人が理解できるようにコードを書いているかのように書く。 – max