Wicked Gem,缺少相关模型参数

Wicked Gem, associated models param is missing

我有一个船模型和一个位置模型。船 has_one :location,位置 belongs_to :boat。我使用 wicked gem 来更新模型。但我在 boat_steps_controller#update 操作中遇到问题。

这是我的 boat_steps_controller,

class BoatStepsController < ApplicationController
    include Wicked::Wizard


    before_action :logged_in_user
    steps :model, :pricing, :description, :picture, :overview, :features, :location 

    def show
        @boat = current_user.boats.find(params[:boat_id])
    case step
    when :location 

        @location = @boat.build_location

    when :picture
        @picture = @boat.pictures.new
        @pictures = @boat.pictures.all
    end
        render_wizard

    end

    def update
        @boat = current_user.boats.find(params[:boat_id])
        @boat.update(boat_params)
    case step
    when :picture
        @picture.update(picture_params)

    when :location

        @location.update(location_params)

    end
        render_wizard @boat


    end


private

    def boat_params
      params.require(:boat).permit(:brand, :year, :model, .....)
    end

    def picture_params
      params.require(:picture).permit(:name, :boat_id, :image)
    end

    def location_params
      params.require(:location).permit(:address, :longitude, :latitude, :formatted_address, :location_type)
    end


end

这里的问题是,在 #update 操作中,我在每一步都更新 boat_params。但是在 Location 中,没有 boat_params 可以更新,因为它是关联模型。所以我必须找到一种方法,要么从表格中获取船号,要么放置 if 语句。

这里是 location.html.erb(wicked gem 的形式)

<%= form_for [@boat, @location], url: wizard_path, method: :put do |f| %>
    <div class="field">
        <%= f.label :address %><br>
        <%= f.text_field :address,:id => "geocomplete", :value => "Ataköy Marina, 34140 Bakırköy/İstanbul, Türkiye" %>
     </div>
     <div class="field">
        <%= f.label :longitude %><br>
        <%= f.text_field :longitude, :name => "lng", :readonly => "readonly" %>
      </div>

    <div class="field">
        <%= f.label :latitude %><br>
        <%= f.text_field :latitude, :name => "lat", :readonly => "readonly" %>
     </div>

    <div class="field">
        <%= f.label :formatted_address %><br>
        <%= f.text_field :formatted_address, :name => "formatted_address",        :readonly => "readonly" %>
    </div>

    <div class="actions">
        <%= f.submit "Finish" ,class: "btn btn-primary" %>
    </div>

<% end %>

它通常应该发送船号,因为我使用 [@boat, @location],url 变成 http://localhost:3000/boats/241/boat_steps/location。但是当我 post 这个时,我得到一个错误;

Started PUT "/boats/241/boat_steps/location" for ::1 at 2015-05-12 10:00:21 +0300
Processing by BoatStepsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jJOEBSCe9WdcuMKiHeVnh9zFEYuu15L5tzIkNFo9cED7ToG0MHq8jqeGstq5krdRGnrNXayNTQI0fajjHsNGgQ==", "location"=>{"address"=>"Ataköy Marina, 34140, Bakırköy, İstanbul, Türkiye"}, "lng"=>"28.87443200000007", "lat"=>"40.971388", "formatted_address"=>"Ataköy Marina, 34140 Bakırköy/İstanbul, Türkiye", "commit"=>"Finish", "boat_id"=>"241", "id"=>"location"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Boat Load (0.2ms)  SELECT  "boats".* FROM "boats" WHERE "boats"."user_id" = ? AND "boats"."id" = ? LIMIT 1  [["user_id", 1], ["id", 241]]
Completed 400 Bad Request in 51ms

ActionController::ParameterMissing (param is missing or the value is empty: boat):
  app/controllers/boat_steps_controller.rb:50:in `boat_params'
  app/controllers/boat_steps_controller.rb:25:in `update'

当我从 #update 操作中删除 @boat.update(boat_params) 时(这是错误的)但随后我收到错误消息,

NoMethodError (undefined method `update' for nil:NilClass):
  app/controllers/boat_steps_controller.rb:32:in `update' 

我只是将一个简单的 else 条件设置为;

def update
        @boat = current_user.boats.find(params[:boat_id])
    case step
    when :picture
        @picture.update(picture_params)

    when :location
        @location = @boat.build_location
        @location.update_attributes(address: params[:location][:address], longitude: params[:lng], latitude: params[:lat])
    else

        @boat.update(boat_params)
    end
        render_wizard @boat


    end