【问题标题】:Issue with form submission in Rails 5.2Rails 5.2 中的表单提交问题
【发布时间】:2019-01-03 14:07:42
【问题描述】:

我有一个当前没有更新的表单。错误信息是:

NoMethodError(nil:NilClass 的未定义方法“更新”):

在控制台中我有: Completed 500 Internal Server Error

控制器

 def edit
    @coffeeshop = Coffeeshop.find(params[:id])
  end

  def update
    respond_to do |format|
      if @coffeeshop.update(coffeeshop_params)
        format.html { redirect_to @coffeeshop, notice: 'Coffeeshop was successfully updated.' }
        format.json { render :show, status: :ok, location: @coffeeshop }
      else
        format.html { render :edit }
        format.json { render json: @coffeeshop.errors, status: :unprocessable_entity }
      end
    end
  end

...

  private
    def coffeeshop_params
      params.require(:coffeeshop).permit(:roaster_id, :name, :desc, :area, :url, :email, :address, :postcode, :locale, :phone, :image_path, :image_thumb_path, :snippet, :beans, :long_black, :tag_list, :slug, :id, :image)
    end

_form

<%= form_with(model: @coffeeshop, local: true) do |form| %>
  <% if @coffeeshop.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@coffeeshop.errors.count, "error") %> prohibited this coffee_bean from being saved:</h2>

      <ul>
      <% @coffeeshop.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <form>
    <div class="form-group">
      <%= form.label :name %><br />
      <%= form.text_field :name, class: "form-control" %>
    </div>
  <br />
    <div class="form-group">
      <%= form.label :snippet %><br />
      <%= form.text_field :snippet, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Description %><br />
      <%= form.text_area :desc, rows:8, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Locale %><br />
      <%= form.text_field :locale, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Area %><br />
      <%= form.text_field :area, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Website %><br />
      <%= form.url_field :url, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Email %><br />
      <%= form.email_field :email, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Address %><br />
      <%= form.text_field :address, class: "form-control" %>
    </div>
<br />
     <div class="form-group">
      <%= form.label :Postcode %><br />
      <%= form.email_field :postcode, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Phone %><br />
      <%= form.telephone_field :phone, class: "form-control" %>
    </div>
<br />
     <div class="form-group">
      <%= form.label :Image %><br />
      <%= form.text_field :image_path, class: "form-control"%>
    </div>
<br />
     <div class="form-group">
      <%= form.label :Thumbnail %><br />
      <%= form.text_field :image_thumb_path, class: "form-control" %>
    </div>
<br />
     <div class="form-group">
      <%= form.label :Roaster %><br />
      <%= form.collection_select(:roaster_id, Roaster.order(:roaster_name), :roaster_id, :roaster_name, :prompt => 'Select Roaster', class: "form-control") %>
    </div>
<br />
    <div class="form-group">
      <%= form.label :Price_of_a_long_black %><br />
      <%= form.number_field :long_black, class: "form-control" %>
    </div>
<br />
    <div class="form-group">
      <%= form.submit "Submit", class: "btn btn-primary" %>
    </div>

  </form>

<% end %>

表单使用 collection_select 从 roasters.rb 模型中提取值,其中:

class Coffeeshop < ApplicationRecord
  belongs_to :roaster

class Roaster < ApplicationRecord
      has_many :coffeeshops

这不保存的任何原因?

我也无法创建新记录,哪里会出现错误提示 roaster must exist

【问题讨论】:

    标签: ruby-on-rails forms


    【解决方案1】:

    问题是 @coffeeshop 没有在你的函数上下文中定义,因此它是 nil 并且不识别更新方法。你应该做的是首先从你的数据库中获取咖啡店,做类似的事情

    @coffeeshop = Coffeeshop.find(params[:id]
    

    之后,咖啡店变量将被定义,您将被允许更新。另一种方法是定义一个私有方法,例如

    def coffeeshop
      @coffeeshop ||= Coffeeshop.find(params[:id]
    end
    

    这样你就可以使用coffeeshop作为方法,如果你已经用过就不会再被搜索了。

    【讨论】:

    • 是的。刚刚将其添加到更新功能中,现在可以发挥作用了!谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 2010-10-09
    • 1970-01-01
    • 2012-08-13
    • 2016-06-14
    • 1970-01-01
    • 2021-05-13
    • 2015-07-09
    相关资源
    最近更新 更多