无法编辑项目 - <%if @ user.errors.any? %GT;

时间:2015-07-15 11:01:38

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我正在使用Simple Form gem。

当我尝试更新带有INVALID数据的项目以进行实例化时,会遗漏一些信息,例如Price。 Rails被假定为重定向到编辑视图。正如控制器所见。

Blow是项目控制器的更新方法

  def update
    @item = Item.find(params[:id])
    if @item.update(item_params)
       redirect_to @item
       flash[:success] = 'Item was successfully updated.'
    else
      flash[:danger] = "Item didn't update"
      render "edit"
    end
  end

然而,它并没有。我刚收到这个错误。

显示

/Users/josep/Documents/Safsy/Website/Safsy/Safsy/app/views/shared/_error_messages.html.erb where line #1 raised:

undefined method `errors' for nil:NilClass

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>

下面是_error.messages.html.erb

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

部分在我的applications.html.erb

中呈现
<!DOCTYPE html>
<html>
  <head>
    <%= stylesheet_link_tag 'application', media: 'all',
                                           'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href='https://fonts.googleapis.com/css?family=Raleway:500,900,200' rel='stylesheet' type='text/css'>

    <!--[if lt IE 9]>
      <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
      </script>
    <![endif]-->
  <title>Safsy - The Safest Way To Shop</title>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <%= yield :sliderhome %>
    <div class="container">
      <% flash.each do |message_type, message| %>
        <div class="alert alert-<%= message_type %>"><%= message %></div>
      <% end %>
      <%= yield %>
    </div>
    <%= render 'layouts/footer' %>
  </body>
</html>

查看/项目/ edit.html.erb

<div class="page-header">
  <h1><%=@item.title  %> <small> Edit</small></h1>
</div>

<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= render 'shared/error_messages' %>
          <%= simple_form_for @item do |f| %>
            <%= f.input :image%>
            <%= f.input :title%>
            <%= f.input :price %>
            <%= f.input :description %>
            <%= f.button :submit, "Save changes", class: "btn btn-primary" %>
          <% end %>
        </div>
      </div>    
    </div>
  </div>
</div>

提前感谢您提供任何帮助

0 个答案:

没有答案