简体   繁体   English

邪恶的宝石,相关模型参数缺失

[英]Wicked Gem, associated models param is missing

I have a Boat model and Location Model. 我有船模型和位置模型。 Boat has_one :location , Location belongs_to :boat . has_one :location ,位置belongs_to :boat I used wicked gem to update the models. 我使用了邪恶的宝石来更新模型。 But I am having an issue in boat_steps_controller 's #update action. 但是我在boat_steps_controller#update动作中有问题。

Here is my boat_steps_controller , 这是我的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

The problem here is that, in #update action, I update boat_params in every step. 这里的问题是,在#update动作中,我在每个步骤中都更新了boat_params But in Location, there is no boat_params to update as it is a associated model. 但是在“位置”中,因为它是关联的模型,所以没有要更新的boat_params So I have to find a way either get the boat id from the form or put if statement. 因此,我必须找到一种方法,从表格中获取船ID或放置if语句。

Here is the location.html.erb (form for wicked gem) 这是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 %>

It should normally send boat id as I use [@boat, @location] , the url becomes, http://localhost:3000/boats/241/boat_steps/location . 当我使用[@boat, @location] ,它通常应该发送出船号,网址变为http://localhost:3000/boats/241/boat_steps/location But when I post this, I get an error of; 但是,当我发布这篇文章时,我得到了一个错误;

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'

And when I erase @boat.update(boat_params) from #update action (which is wrong) but then I receive an error, 当我从#update动作中删除@boat.update(boat_params) (这是错误的),但是随后我收到一个错误,

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

I just put an easy else condition as; 我只是提出了一个简单的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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