ruby on rails - Wicked Gem, associated models param is missing -
i have boat model , location model. boat has_one :location
, location belongs_to :boat
. used wicked gem update models. having issue in boat_steps_controller
's #update
action.
here 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 that, in #update
action, update boat_params
in every step. in location, there no boat_params
update associated model. have find way either boat id form or put if statement.
here location.html.erb
(form wicked gem)
<%= form_for [@boat, @location], url: wizard_path, method: :put |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 send boat id use [@boat, @location]
, url becomes, http://localhost:3000/boats/241/boat_steps/location
. when post this, error of;
started put "/boats/241/boat_steps/location" ::1 @ 2015-05-12 10:00:21 +0300 processing boatstepscontroller#update 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".* "users" "users"."id" = ? limit 1 [["id", 1]] boat load (0.2ms) select "boats".* "boats" "boats"."user_id" = ? , "boats"."id" = ? limit 1 [["user_id", 1], ["id", 241]] completed 400 bad request in 51ms actioncontroller::parametermissing (param missing or value empty: boat): app/controllers/boat_steps_controller.rb:50:in `boat_params' app/controllers/boat_steps_controller.rb:25:in `update'
and when erase @boat.update(boat_params)
#update
action (which wrong) receive error,
nomethoderror (undefined method `update' nil:nilclass): app/controllers/boat_steps_controller.rb:32:in `update'
i put easy else
condition as;
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
Comments
Post a Comment