I have a controller action that requires parameters, and has to be accessed both through get and post. with get it looks like:
get 'cart/add:id,:qty' => 'cart#add', as: :addToCart
the controller action is:
def add
product = Product.find(params[:id])
if ::AddToCart.new(@cart, product, params[:qty]).execute
flash[:succes] = "Product(s) added."
else
flash[:failure] = "Product cannot be added."
end
redirect_to request.referer
end
how should the post route look like if i would like it to use the same action.
PS: i use the get version as a link_to when only one product is added, the post route is needed because the quantity will be unknown
You should make this strictly a post route. If you want, you can default the quantity to one when it isn't provided. Or, use ajax to post to the route with a quantity of one when they click the "add one" link. That way it can look and seem to behave like a regular link_to sort of link, but you'll still have the benefits of only updating data via POST and leaving all your actual GET requests idempotent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.