【问题标题】:Wicked Gem, associated models param is missing邪恶的宝石,缺少相关的模型参数
【发布时间】:2015-07-22 21:42:45
【问题描述】:

我有一个船模型和一个位置模型。船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,因为它是关联模型。所以我必须找到一种方法,要么从表格中获取船 ID,要么放入 if 语句。

这是location.html.erb(邪恶宝石的表格)

<%= 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] 时,它通常应该发送船ID,URL 变为http://localhost:3000/boats/241/boat_steps/location。但是当我发布这个时,我得到一个错误;

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' 

【问题讨论】:

    标签: ruby-on-rails controller wizard


    【解决方案1】:

    我只是把一个简单的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多