ryan42 (11) [Avatar] Offline
#1
Just wanted to share two issues I was running into and my fixes for them in case anyone else has the same problems:

When a user without permission to change the state on a project added a comment, the state would be sent as nil, and that makes the current state of the ticket nil which is not good. In comments_controller, I put the following code in the create method,

if cannot?(:"change states", @ticket.project)
params[:comment].delete(:state_id)
#my addition, to set back to default after user's choice was removed
params[:comment][:state_id] = State.find_by_default(true)
end

I also had tags not being saved when editing existing tickets.On the tickets_controller, I put this inside the edit method, which fixed the problem:

if can?(:tag, @ticket.project) || current_user.admin?
@ticket.tag!(params[:tags])
end
ryan42 (11) [Avatar] Offline
#2
Re: editing ticket and adding comments to ticket, 2 small bugs w/ my fixes
in regards to the 1st issue above, I ran into a problem where the project ID was not found. This was possibly because of something I changed. My comments controller create method now looks like the following and I am working again:

def create
if cannot?(:"change states", @ticket.project)
params[:comment].delete(:state_id)
params[:comment][:state_id] = State.find_by_default(true)
end

@comment = @ticket.comments.build(params[:comment].merge(:user => current_user))

if @comment.save
if can?(:tag, @ticket.project) || current_user.admin?
@ticket.tag!(params[:tags])
end
flash[:notice] = "Comment has been created."
redirect_to [@ticket.project, @ticket]
else
@states = State.all
flash[:alert] = "Comment has not been created."
# added the line here to get the project_id that was missing
@project = @ticket.project
render :template => "tickets/show"
end
end