I’ve added a resource route to my app using –
resources :cars
I’ve used scaffolding to do this
I’ve then changed the routes so the user can edit a car by using the following url – mydomain.com/CARNAME
match "/:car" =>"cars#edit", :as => :edit_car
match "/:car/thanks" =>"cars#thanks", :as => :thanks
My edit action looks like this –
def edit
@car = Car.find_by_name(params[:car]) || not_found
end
My problem is that in the edit page (mydomain.com/CARNAME) renders a form like this –
<form accept-charset="UTF-8" action="/cars/2" class="edit_car" id="edit_car_2" method="post">
I want to hide the ID of the car I’d prefer it to look like this –
<form accept-charset="UTF-8" action="/cars/CARNAME" class="edit_car" id="edit_car_CARNAME" method="post">
My update action is like this at the moment –
def update
@car = Car.find(params[:id]) #I'll have to change this to - Car.find_by_name(params[:name])
if @car.update_attributes(params[:car])
format.html { redirect_to(thanks_path(@car.name)) }
else
format.html { render action: "edit" }
end
end
I’d also like the update action to redirect back to the edit action (mydomain.com/CARNAME) and continue to pass through any validation issues.
Try overriding the method
to_paramin your Car model:The view should then render the url you’re expecting.