简体   繁体   中英

Issue with form submission in Rails 5.2

I have a form that isn't currently updating. The error message is:

NoMethodError (undefined method `update' for nil:NilClass):

In the console I have: Completed 500 Internal Server Error

controller

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

The form uses a collection_select to pull values from roasters.rb model where:

class Coffeeshop < ApplicationRecord
  belongs_to :roaster

and

class Roaster < ApplicationRecord
      has_many :coffeeshops

Any reason why this isn't saving?

I also can't create a new record, where I can an error saying roaster must exist ?

The problem is that @coffeeshop is not defined in the context of your function, therefore is nil and does not recognize the update method. What you should do is first fetch the coffeeshop from your database, doing something like

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

After that, the coffeeshop variable will be defined and you will be allowed to update. An alternative is defining a private method such as

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

this way you can use coffeeshop as a method and won't be searched again if you have already used it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM