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

时间:2014-01-16 17:05:56

标签: ruby-on-rails ruby database ruby-on-rails-3.2 crud

所以我刚刚在我的网站上完成了“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

STOCKLIST VIEW

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

A link to a picture of the problem

THE NEWITEM VIEW

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

The Code on the server when it ran

1 个答案:

答案 0 :(得分:0)

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

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

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

你有没有理由不使用rails默认的RESTfull路由?