创建新项目并将其放入数据库

时间:2017-09-26 15:38:45

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

我正在尝试创建新对象并将其存储到数据库中,但我无法理解为什么会出现以下错误:在这里您可以看到错误:

Started POST "/cruises" for 127.0.0.1 at 2017-09-26 16:34:59 +0100
Processing by CruisesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OkFmCPhl0pjFezWCsxp+c16wr3j9nbsdLIdgj+PsCMgSDmTzBoEJQ69tB8IA3uNayLRO+LMTi/73YqYCMImtCA==", "cruise"=>{"name"=>"NewAsasa", "ship_id"=>""}, "commit"=>"Create Cruise"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendering cruises/new.html.erb within layouts/application
  Rendered cruises/_form.html.erb (3.6ms)
  Rendered cruises/new.html.erb within layouts/application (4.4ms)
Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.1ms)



ActionView::Template::Error (undefined method `map' for nil:NilClass
Did you mean?  tap):
    18: 
    19:   <div class="field">
    20:     <em><%= f.label :ship_id %></em>
    21:      <%= f.collection_radio_buttons( :ship_id, @ships, :id, :name ) %>
    22:   </div>
    23: 
    24:   <div class="actions">

CruiseController - “”在这里你可以看到更多关于下面的控制器:我只需要写更多的文本,因为当我尝试放置这段代码时它给了我错误,所以不要在这里阅读这个解释“”

class CruisesController < ApplicationController
  before_action :set_cruise, only: [:show, :edit, :update, :destroy]
  rescue_from ActiveRecord::RecordNotFound, with: :redirect_if_not_found

  # GET /cruises
  # GET /cruises.json
  def index
    @cruises = Cruise.all
  end

  # GET /cruises/1
  # GET /cruises/1.json
  def show
  end

  # GET /cruises/new
  def new
    @cruise = Cruise.new
    @ships = Ship.all
  end

  # GET /cruises/1/edit
  def edit
     @ships = Ship.all
  end

  # POST /cruises
  # POST /cruises.json
  def create
    @cruise = Cruise.new(cruise_params)

    respond_to do |format|
      if @cruise.save
        format.html { redirect_to @cruise, notice: 'Cruise was successfully created.' }
        format.json { render :show, status: :created, location: @cruise }
      else
        format.html { render :new }
        format.json { render json: @cruise.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /cruises/1
  # PATCH/PUT /cruises/1.json
  def update
    respond_to do |format|
      if @cruise.update(cruise_params)
        format.html { redirect_to @cruise, notice: 'Cruise was successfully updated.' }
        format.json { render :show, status: :ok, location: @cruise }
      else
        format.html { render :edit }
        format.json { render json: @cruise.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cruises/1
  # DELETE /cruises/1.json
  def destroy
    @cruise.destroy
    respond_to do |format|
      format.html { redirect_to cruises_url, notice: 'Cruise was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_cruise
      @cruise = Cruise.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def cruise_params
      params.require(:cruise).permit(:name, :ship_id)
    end


end

2 个答案:

答案 0 :(得分:0)

无论你在控制器中迭代什么,都是零。根据您显示的代码,我假设它是@ships。我敢打赌,如果你把puts @ships放在你的控制器中它会记录为零。

答案 1 :(得分:0)

您的保存在create操作(回滚事务)中失败,因此if操作的create条件会触发render :new

但是,它是一个渲染,而不是重定向,表示new动作未执行,并且@cruise和@ship都不存在(未定义)。

最后,在呈现new视图时,会触发错误,因为@ships未定义。

编辑:我犯了一个错误,@ ships不是零,它只是不存在(未定义)。