Wednesday, May 16, 2012

Cannot save tag_list when using multi-step forms

I have a belonging model that allows user to post objects to be sold or rented on my website. I recently changed the form, making it a multi-step form: a first form asks the name of the object and if the object is for sale or to rent, a second form ask for object's details, with the fields depending on the user's choice.



I am using is_taggable, with Rails 3.0.5, and my problem is that tag_list is never saved in the database since I switched to the multi-step form (all the other fields are saved correctly).



I followed Ryan Bates Rails cast #217.



Before, I was using: @belonging.tag_list = params[:belonging][:tag_list]



Since I went from multistep, I am using: @belonging.tag_list = session[:belonging_params][:tag_list]



I am a bit of a newbie in Rails, so there might be something obvious I am missing here. I spent the whole afternoon and evening trying to understand what is wrong, any help will therefore be appreciated!



Here are the 'new' and 'create' action of my controller:



class BelongingsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :with_tag, :remove_tag]
after_filter :update_tag_cloud, :only => [:create, :update]

def new
@title = "Insert a new product or service"
@user = current_user
session[:belonging_params] ||= {}
session[:belonging_step] = nil
@belonging = @user.belongings.new(session[:belonging_params])
session[:belonging_params][:tag_list] ||= []
@belonging.current_step = session[:belonging_step]
render 'new'
end

def create
session[:belonging_params].deep_merge!(params[:belonging]) if params[:belonging]
@belonging = current_user.belongings.build(session[:belonging_params])
@belonging.current_step = session[:belonging_step]
@belonging.tag_list=session[:belonging_params][:tag_list]
if params[:previous_button]
@belonging.previous_step
render 'new'
elsif params[:cancel_button]
session[:belonging_step] = session[:belonging_params] = nil
redirect_to user_path(current_user)
elsif params[:continue_button]
if @belonging.last_step?
if @belonging.save!
expire_fragment('category_list')
flash[:success] = "New product or service created!"
session[:belonging_step] = session[:belonging_params] = nil
redirect_to belonging_path(@belonging)
else
flash[:error] = "Object could not be saved"
render 'new'
end
else
@belonging.next_step
render 'new'
end
else
render 'new'
end
session[:belonging_step] = @belonging.current_step
end


Many many thanks for any clue !!





No comments:

Post a Comment