Question

So I had just finished the "CRU" part of "CRUD" on my website. It worked at this point.

I put in the code for the delete section ( I literally did nothing else ) and suddenly instead of saving to the database it simply returns blank fields. I don't understand what on earth is causing it and I simply cannot fix it. I even tried retyping the document.

It clearly reads the database but when it places values into the database they are blank. It have no idea what caused it and I could really do with a more experienced set of eyes spotting the issue.

THE CONTROLLER

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

THE 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

Was it helpful?

Solution

You are not providing any object to bind fields to. Shouldn't your form_for be:

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

As a result of previous :items, form fields where placed in params[:items], so you didn't use them in the controller. In addition it would prevent you from rendering the from for edit action.

Is there any reason why you are not using rails default RESTfull routes?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top