如何从ROR中解决NoMethodError?

时间:2015-03-02 11:58:33

标签: ruby ruby-on-rails-3.2

任何人都可以帮我解决以下错误。

错误:

NoMethodError in Homes#managebooks

Showing C:/Site/library_management1/app/views/homes/managebooks.html.erb where line #48 raised:

undefined method `book_name' for #<Array:0x229d340>

实际上我想在提交后将所有数据提取到现有表中。当我点击提交按钮时,所有数据都保存到数据库中,但之后就抛出了上述错误。请尽快帮助解决这些错误。 / p>

我的代码如下。

的观点/舍/ managebooks.html.erb

<% if admin_signed_in? %>
<div class="bar">
  Logged in as <strong><%= current_admin.email %></strong>.
  <%= link_to 'Edit profile', edit_admin_registration_path, :class => 'navbar-link' %> |
  <%= link_to "Logout", destroy_admin_session_path, method: :delete, :class => 'navbar-link'  %>
</div>
<% else %>
  <%= link_to "Sign up", new_admin_registration_path, :class => 'navbar-link'  %> |
  <%= link_to "Login", new_admin_session_path, :class => 'navbar-link'  %>
<% end %>
<div class="big-container">
    <% if flash[:color]== "valid" %>
        <div class="valid_notice">
            <p><%= flash[:notice]%></p>
        </div>
    <% elsif flash[:color]== "invalid"%>
        <div class="invalid_notice">
            <p><%=flash[:notice]%></p>
        </div>
    <%else%>
        <div class="notice">
            <p><%=flash[:notice]%></p>
        </div>
    <%end%>
<div class="admin-image">
 <div class="bpaddingdiv1"><img src="/assets/admin.png" border="0" name="admin" /></div>
</div>
<div class="borderlightgreen"></div>
<div class="admin-name">
<div class="tpaddingdiv2 textaligncenterdiv"><img src="/assets/adminpanel.png" border="0" name="admin" /></div>
</div>
<div class="leftside">
<div id="leftsidebtn">
 <ul>
<li><a href="/homes/managebooks">Manage Books</a></li>
<li><a href="#" >Manage Pages</a></li>
<li><a href="#" >Manage Header Banner</a></li>
<li><a href="#" >Brand</a></li>
 </ul>  
</div>
</div>
<div class="middlebox">
<center>
    <%= form_for :books,:url => {:action => "savebooks"} do |f| %>
    <fieldset>
         <p>
            <label for="name">Book name</label>
            <%= f.text_field :book_name,placeholder:"enter book name" %>
         </p>

         <p>
            <label for="tel">Book title</label>
            <%= f.text_field :book_title,placeholder:"enter book title" %>
         </p>

         <p>
            <label for="email">Author Name</label>
           <%= f.text_field :author_name,placeholder:"enter book author name" %>
         </p>

         <p>
            <label for="password">publisher Name</label>
            <%= f.text_field :publisher_name,placeholder:"enter book publisher name" %>
         </p>
         <p>
            <label for="password"> Edition</label>
           <%= f.text_field :edition,placeholder:"enter book edition" %>
         </p>
         <p>
            <label for="password">Pages</label>
           <%= f.text_field :pages,placeholder:"enter book pages" %>
         </p>
         <p>
            <label for="password"> Date of pursase</label>
            <%= f.date_select :date_of_purchase %>
         </p>

         <p>
            <%= f.submit "SUBMIT" %>
         </p>
    </fieldset>
 <% end %>
</center>
<% if params[:id] %>
<table>
<tr>
<th>Book name</th>
<th>Book title</th>
<th>Author Name</th>
<th>publisher Name </th>
<th>Edition</th>
<th>Pages</th>
<th>Date of pursase</th>
<th>Status</th>
<th>Action</th>
</tr>
<tr>
    <td><%= @books.book_name %></td>
    <td><%= @books.book_title %></td>
    <td><%= @books.author_name %></td>
    <td><%= @books.publisher_name %></td>
    <td><%= @books.edition %></td>
    <td><%= @books.pages %></td>
    <td><%= @books.date_of_purchase  %></td>
    <td><%= image_tag("/assets/1.png") %></td>
    <td><%= link_to(
      image_tag(
        "/assets/logout.png", 
        :alt => "image", :width => 40, :height => 40, :title => "Delete item"
      ),
      homes_remove_path(:id => @books.id),
      :method => :delete, :confirm => "Are you sure to remove this?") %>
  </td>
    </tr>
</table>
<% end  %>
</div>
</div>

控制器/ homes_controller.rb

class HomesController < ApplicationController
     before_filter :authenticate_admin!,only: [:admin]
    def index

    end
    def admin

    end
    def managebooks
        @books=Book.new
        if params[:id]
            @books=Book.all
        end
    end
    def savebooks
        @books=Book.new(params[:books])
        if @books.save
            flash[:notice]="Data has submitted successfully"
            flash[:color]="valid"
            redirect_to :action => 'managebooks',:id => @books.id
        else
            flash[:notice]="Data couldnot submitted successfully"
            flash[:color]="invalid"
            render 'managebooks'
        end
    end
    def remove
        @books=Book.find(params[:id])
        @books.destroy
    end
end

1 个答案:

答案 0 :(得分:0)

更改erb代码的此部分:

<tr>
  <td><%= @books.book_name %></td>
  <td><%= @books.book_title %></td>
  <td><%= @books.author_name %></td>
  <td><%= @books.publisher_name %></td>
  <td><%= @books.edition %></td>
  <td><%= @books.pages %></td>
  <td><%= @books.date_of_purchase  %></td>
  <td><%= image_tag("/assets/1.png") %></td>
  <td><%= link_to(
    image_tag(
      "/assets/logout.png", 
      :alt => "image", :width => 40, :height => 40, :title => "Delete item"
    ),
    homes_remove_path(:id => @books.id),
    :method => :delete, :confirm => "Are you sure to remove this?") %>
  </td>
</tr>

到此:

<% @books.each do |book| %>
  <tr>
    <td><%= book.book_name %></td>
    <td><%= book.book_title %></td>
    <td><%= book.author_name %></td>
    <td><%= book.publisher_name %></td>
    <td><%= book.edition %></td>
    <td><%= book.pages %></td>
    <td><%= book.date_of_purchase  %></td>
    <td><%= image_tag("/assets/1.png") %></td>
    <td><%= link_to(
      image_tag(
        "/assets/logout.png", 
        :alt => "image", :width => 40, :height => 40, :title => "Delete item"
      ),
      homes_remove_path(:id => book.id),
      :method => :delete, :confirm => "Are you sure to remove this?") %>
    </td>
  </tr>
<% end %>

如果您在managebooks控制器中查看方法homes,则Book中存在:id时会创建一个params个对象数组。

注意:上述更改将再次失败,因为我们在each上使用@books方法,如果没有Book,则没有为新的:id对象定义在params