samedi 25 avril 2015

Wicked, the second can not get the correct path (show action)


So I am trying to build a wicked wizard. I have User and Boat Model. When Boat model is created, all the steps starts. The first #show action works fine for the first step. But I have a problem with the `second step.

Here is my

    #routes
     resources :users
      resources :boats, except: :destroy do
          resources :pictures
      end


  resources :boat_steps 



#boat_steps controller

class BoatStepsController < ApplicationController
    include Wicked::Wizard


    before_action :logged_in_user
    steps :model, :pricing, :description, :picture

    def show
        @boat = current_user.boats.last
        render_wizard
    end

    def update
        @boat = current_user.boats.last
        @boat.update_attributes(params[:boat])
        render_wizard @boat
    end


end

#model.html.erb
<% provide(:title, "List Boat") %>
<h1>List your Boat</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <p>AHoy! <%= current_user.name %>. It only takes 5 mins to fill the form!.</p>
  </br></br>

  </br>
    <%= form_for(@boat, url: wizard_path, method: put) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>

      <h2>Brand & Model</h2>
      <%= f.label :brand %>
      <%= f.text_field :brand, placeholder: "Brand Name", class: 'form-control' %>

      <%= f.label :model %>
      <%= f.text_field :model,  placeholder: "Model", class: 'form-control' %>

      <%= f.label :boat_category, "Boat Category" %>

      <% if @boat.boat_type == "Power" %>
      <%= f.collection_select(:boat_category,  Power.all, :name, :name, {:prompt   => "Select a Category"}) %>
      <% else %>
      <%= f.collection_select(:boat_category,  Sail.all, :name, :name, {:prompt   => "Select a Category"}) %>
      <% end %>


      <% if @boat.boat_type == "Sail" %>
      <%= f.label :mast_material, "Mast Material" %>
      <%= f.collection_select(:mast_material,  Mast.all, :name, :name, {:prompt   => "Select a Material"}) %>
      <% end %>
      <%= f.label :hull_material, "Hull Material" %>
      <%= f.collection_select(:hull_material,  Hull.all, :name, :name, {:prompt   => "Select a Material"}) %>


      <%= f.label :boat_length, "Boat Length" %>
      <%= f.number_field :boat_length, placeholder: "Ex. 15", max: '50',  class: 'form-control' %>


      <%= f.submit "Next", class: "btn btn-primary" %>
    <% end %>


  </div>
</div>

And here is the

#pricing.html.erb
<h2>Pricing</h2>

<%= form_for (@boat, url: wizard_path) do |f| %>
      <%= f.radio_button(:captained, "Captained") %>
      <%= f.label(:captained_captained, "Captained") %>
      <%= f.radio_button(:captained, "Bareboat") %>
      <%= f.label(:captained_bareboat, "Bareboat") %>

      <%= f.label :daily_price, "Daily Price (TL)" %>
      <%= f.number_field :daily_price, placeholder: "Ex. 180.00", max: '1000.00',  class: 'form-control' %>

      <%= f.submit "Next", class: "btn btn-primary" %>
 <% end %>

Lastly,

#boats controller
 def new
    @boat = Boat.new
  end

  def create
   @boat = current_user.boats.new(boat_params) if logged_in?
    if @boat.save
      #flash[:success] = "Continue from here"
      redirect_to boat_steps_path
    else
      render 'new' 
    end
  end

  def show
    @boat = Boat.find(params[:id])

  end


  def edit
    @boat = Boat.find(params[:id])
  end


  def update
     @boat = Boat.find(params[:id])
    if @boat.update_attributes(boat_params)
      flash[:success] = "The Boat Saved"
      redirect_to root_path
    else
      render 'edit'
    end
  end

So, the first form, (model.html.erb) opens as; http://localhost:3000/boat_steps/model, when I click next, the url becomes;

http://localhost:3000/boat_steps/id=157 

It is because of the #show action (@boat = current_user.boats.last) I believe but I could not find a way to do it. If I make it @boat = current_user.boats then I receive undefined method to_key'

Here is the log when I use ; @boat = current_user.boats.last

Started PATCH "/boat_steps/model" for ::1 at 2015-04-25 19:48:39 +0300
Processing by BoatStepsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"m5MY9OjdHLLplnzHyO5VDmLwx8hboMOKrtPRGuBnTNzsTh1F+DlVWxKoDL9smYXYpE8bHln6HHEtnF3NpJl6HQ==", "boat"=>{"brand"=>"A & M Manufacturing Inc", "model"=>"LAGOON 14 SPORT/FO [14']", "boat_category"=>"", "mast_material"=>"", "hull_material"=>"", "boat_length"=>"31"}, "commit"=>"Next", "id"=>"model"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Boat Load (0.1ms)  SELECT  "boats".* FROM "boats" WHERE "boats"."user_id" = ?  ORDER BY "boats"."id" DESC LIMIT 1  [["user_id", 1]]
Completed 500 Internal Server Error in 4ms

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
  app/controllers/boat_steps_controller.rb:15:in `update

Actually, the url should become, http://localhost:3000/boat_steps/pricing

thank you


Aucun commentaire:

Enregistrer un commentaire