I am running rails 3.2
I have created a nested form (requests > tags) with coffeescript handling the addition of new tags.
Everything works with the exception of the form posting a blank tag.name
I am trying to write a method to delete the blank field before the form posts. I realize this may be the wrong approach, but I am still a beginner:
requests_controller.rb
def create
@request = current_user.requests.build(params[:request])
@tag = Tag.new
if @tag.name.blank?
destroy_blank
end
respond_to do |format|
if @request.save
format.html { redirect_to(@request,
:notice => 'Request was successfully created.') }
format.json { render :json => @request,
:status => :created, :location => @request }
else
format.html { render :action => "new" }
format.json { render :json => @request.errors,
:status => :unprocessable_entity }
end
end
end
request.rb
def destroy_blank
blank = @tag.name
blank.delete
end
I hope that’s clear. If not let me know and I will include more information.
If you can’t stop blank tags from coming in, you can create a before_create filter in the model to skip saving blank tags. Leave the controller clean and simple.
Good luck!