Rails project: Project
has many Ticket
's.
Path to editing a ticket: /projects/12/tickets/11/edit
When updating a Ticket and validation fails, I use render :action => "edit"
.
However, when the edit view renders this time, the path changes to /tickets/11/
Which means I lose some parameters. How can I keep the original path?
routes.rb:
resources :projects do
resources :tickets
end
resources :tickets
tickets_controller.rb
def new
@ticket = Ticket.new
end
def create
@ticket = Ticket.new(params[:ticket])
@ticket.user_id = session[:user_id]
respond_to do |format|
if @ticket.save
format.html { redirect_to project_path(@ticket.project), :notice => "Ticket was created." }
else
format.html { render :action => "new" }
end
end
end
def edit
@ticket = Ticket.find(params[:id])
end
def update
@ticket = Ticket.find(params[:id])
respond_to do |format|
if @ticket.update_attributes(params[:ticket])
format.html { redirect_to project_ticket_path(@ticket.project, @ticket), :notice => "Ticket was updated." }
else
format.html { render :action => "edit" }
end
end
end