网站未更新数据库 [Ruby on Rails]



所以我刚刚在我的网站上完成了"CRUD"的"CRU"部分。它在这一点上起作用了。

我输入了删除部分的代码(我实际上什么也没做),突然而不是保存到数据库中,它只是返回空白字段。我不明白到底是什么原因造成的,我根本无法修复它。我什至尝试重新输入文档。

它清楚地读取数据库,但是当它将值放入数据库时,它们是空白的。它不知道是什么原因造成的,我真的可以用更有经验的眼睛来发现这个问题。

控制器

class ItemsController < ApplicationController
layout 'admin'
  def index
    stocklist
    render('stocklist')
  end
  def stocklist
    @items = Item.order("items.position ASC")
  end
  def itemdetails
    @item = Item.find(params[:id])
  end
  def newitem
    @item = Item.new  
  end
  def create 
        # Make a new item 
        @item = Item.new(params[:item])
        #Save it
        if @item.save
        #If it succeeds then
        flash[:notice] = "Item Created"
        redirect_to(:action => 'stocklist')
        # if it fails then      
        else
            render('newitem')
        end
  end
    def edit
      @item = Item.find(params[:id])
    end
   def  update
        # find the old subject 
        @item = Item.find(params[:id])
        #update it
        if @item.update_attributes(params[:item])
        #If it succeeds then
        flash[:notice] = "Item Updated"
        redirect_to(:action => 'itemdetails', :id => @item.id)
        # if it fails then      
        else
            render('edit')
        end
    end
    def delete
      @item = Item.find(params[:id])
    end
    def destroy
      item = Item.find(params[:id]).destroy
      flash[:notice] = "Item Destroyed"
      redirect_to(:action => 'stocklist')
    end

end

库存清单视图

<%  @page_title = "Items List" %>
<div class="item list">
 <h2>Items</h2>
<%= link_to("Add New item", {:action => 'newitem'}, :class => 'action newitem') %>
 <table class="listing" summary="item list">
    <tr class="header">
        <th>&nbsp;</th>
        <th>Item</th>
        <th>Description</th>
        <th>Price</th>
        <th>Visible</th>
        <th>Actions</th>
    </tr>
    <% @items.each do |item| %> 
       <tr>
        <td><%= item.position %></td>
        <td><%= item.name %></td>
        <td><%= item.description %></td>
        <td> £ <%= item.price %></td>
        <td class="center"><%= item.visible ? 'Yes' : 'No' %></td>
        <td class="actions">
          <%= link_to ("Details", {:action => 'itemdetails', :id => item.id}, :class => 'action itemdetails') %> 
          <%= link_to ("Edit", {:action => 'edit', :id => item.id}, :class => 'action edit') %> 
          <%= link_to ("Delete", {:action => 'delete', :id => item.id}, :class => 'action delete') %>
        </td>
    </tr>
    <% end %> 
 </table>
</div>

指向问题图片的链接

"新项目"视图

<%= link_to("<< Back to Stocklist", {:action => 'stocklist'}, :class => 'back-link') %>
<div class="item new">
  <h2>Create Item</h2>
  <%= form_for(:items, :url => {:action => 'create'}) do |f| %>
     <%= render(:partial => "form", :locals => {:f => f}) %>
    <div class="form-buttons">
      <%= submit_tag("Create Item") %>
    </div>
  <% end %>
</div>

运行时服务器上的代码

您没有提供任何要将字段绑定到的对象。你的form_for不应该是:

form_for(@item, :url => {:action => 'create'})

由于之前的:items,表单字段放置在params[:items]中,因此您没有在控制器中使用它们。此外,它还会阻止您呈现 from for edit 操作。

有什么理由不使用rails默认的RESTfull路由吗?

最新更新