I have a user model and boat model. They are associated. The thing is, when user creates a boat, the #create action redirects user to #edit page. In #edit page I have a long form that I want to use for wicked. Every video I watched, they used wicked gem after #create action. So I do not know if I can use it for the edit page. I have tried but encounter with an error.
boat_steps controller
class BoatStepsController < ApplicationController
include Wicked::Wizard
steps :pricing, :description, :picture
def show
@boat = current_user.boats
render_wizard
end
end
Boats controller
class BoatsController < ApplicationController
before_action :logged_in_user, only: [:new, :show, :edit, :update]
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"
render 'edit'
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 boat_steps_path(@boat)
else
render 'edit'
end
end
def update_years
# updates year and model based on brand selected
brand = Brand.find_by_name(params[:brand_name])
# map to name and id for use in our options_for_select
@years = brand.years.map{|a| [a.name, a.name]}.insert(0, "Select a Year") #use a.name here instead of a.id
@models = brand.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model")#use s.name here instead of s.id
end
def update_models
# updates model based on year selected
year = Year.find_by_name(params[:year_name])
@models = year.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model") #use s.name here instead of s.id
end
private
def boat_params
params.require(:boat).permit(:brand, :year, :model, :captained, :boat_type, :daily_price, :boat_length, :listing_tagline, :listing_description, :boat_category, :hull_material, :mast_material)
end
end
The edit page works fine. Then when I click next to get the pricing step I get error, it might because of the boat_steps controller' s #show action or the from in the pricing.html.erb. But I could not find a solution. Here is the pricing.html.erb
<h2>Pricing</h2>
<%= form_for @boat, url: wizard_path do |f| %> <!-- HERE PROBLEM-->
<%= 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 %>
So I get this error;
NoMethodError in BoatStepsController#show
undefined method `to_key' for #<Boat::ActiveRecord_Associations_CollectionProxy:0x007f85a5291fa8>
Or should I use wicked after #create action, instead of #edit action?
Aucun commentaire:
Enregistrer un commentaire